views:

170

answers:

2

Hi everyone,

This is my first post on stackoverflow so please be patient :-)

I have an Json Array of Arrays:

 {
  "response_type": "ok",
  "total_results": 1,
  "page": 1,
  "total_pages": 1,
  "sets": [
    {
      "id": 2075184,
      "created": 1269989599,
      "term_count": 5,
      "has_images": false,
      "terms": [
        [
          "\u9031\u672b",
          "Weekend",
          ""
        ],
        [
          "\u65e5\u66dc\u65e5",
          "Sunday",
          ""
        ],
        [
          "\u571f\u66dc\u65e5",
          "Saturday",
          ""
        ],
        [
          "\u79c1",
          "I, myself",
          ""
        ],
        [
          "\u65e5\u672c\u8a9e",
          "Japanese",
          ""
        ]
      ]
    }
  ]
}

I am using this code in my project to get the "terms" into an NSDictionary:

  NSURL *url = [NSURL URLWithString:@"http://localhost/json"];

  NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];  
  NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
  NSError *error = nil;
  NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];

  NSLog(@"dict1: %@", dict);
  if (dict)
  {
    rows = [dict objectForKey:@"sets"];
  }

  NSLog(@"Array: %@", rows);

However I am having trouble figuring out how to get the terms separated for example:

[
          "\u9031\u672b",
          "Weekend",
          ""
        ],

I would like an NSArray of all the Japanese: "\u9031\u672b"(that is japanese btw) and an NSArray of all the definitions Weekend (Or a NSDictionary). However I am not sure how to separate them. We can safely assume that all the Japanese is 0 and all the definitions are 1. Any help would be wonderful!

thank you,

+2  A: 

Have you considered using the JSON Framework for Objective-C iPhone?

John Wang
I have, can it handle this better? Any tutorials or examples would be wonderful.
leachianus
@leachianus - `NSDictionary * dict = [jsonreturn JSONValue];` There's your tutorial.
Dave DeLong
what is JSONValue?
leachianus
@leachianus a method provided by the JSON framework. It's how you do all the json parsing.
Dave DeLong
Ah, so I see how much easier that is: NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; NSDictionary * dict = [jsonreturn JSONValue]; NSLog(@"%@", dict);However I have arrived at the same problem, How do I take the terms and separate them?thank you,
leachianus
did you figure it out? the NSDictionary can contain NSDictionaries inside it, so for your example, you'd have a String value of "ok" in the dictionary with the key "response_type" and also a dictionary called "set" in the dictionary. which would be a nested NSDictionary. Looking at a plist file may help you better understand if that's confusing.
John Wang
A: 
    [
    "\u9031\u672b",
    "Weekend",
    ""
    ],

I would like an NSArray of all the Japanese: "\u9031\u672b"(that is japanese btw) and an NSArray of all the definitions Weekend (Or a NSDictionary). However I am not sure how to separate them. We can safely assume that all the Japanese is 0 and all the definitions are 1.

So element 0 (and likewise element 1) may be a string or an array, and if it's not an array, you want to wrap it in an array?

Just do that, then. Get element 0, test whether it's an array, and wrap it in one if it isn't. Then, do the same for element 1.

You might consider making some changes on the server side, if you can: Serve up the data as a dictionary with keys chosen for clarity, rather than an array with magic indexes, and have any values that are zero-or-more or one-or-more always be arrays.

Peter Hosey