Pergunta

I want to parse a JSON file in my iphone app. The problem is i can parse simple json files but i am confused how to do parsing on following type of json:

[{"123":  
  [{ "item_id":"222",  
     "image_count":"2",  
     "image_filetype":".jpg",  
     "image_url":"http:\/\/someurl.jpg",  
  },  
  {"item_id":"333",  
     "image_count":"2",  
     "image_filetype":".jpg",  
     "image_url":"http:\/\/someurl.jpg",  
  }]  
}]  

Can some on help me how to extract all the img_urls for "123".

Thank you.

Foi útil?

Solução

NSString *jsonString = …;

// The top-level object is an array
NSArray *array = [jsonString JSONValue];

// The first element in the array is an object containing a name-value
// pair for the key/name "123". The value is itself an array
NSArray *itemsIn123 = [[array objectAtIndex:0] objectForKey:@"123"];

// Use Key-Value Coding to get an array of all values for the key
// image_url
NSArray *imgurls = [itemsIn123 valueForKey:@"image_url"];


Edit based on comments:

Since the top-level array may consist of several objects, each object having a single name-value pair with unknown name, you need to manually iterate over the top-level array:

NSString *jsonString = …;

NSMutableArray *imgurls = [NSMutableArray array];

// The top-level object is an array
NSArray *array = [jsonString JSONValue];

// Each element in the top-level array is an object
for (NSDictionary *outerObject in array) {
    // Iterate over all values in the object. Each (single) value is an array
    for (NSArray *innerArray in [outerObject allValues]) {
        [imgurls addObjectsFromArray:[innerArray valueForKey:@"image_url"]];
    }
}

Outras dicas

The value for the object "123" will be an NSArray of NSDictionaries. Each of these dictionaries has a key "image_url" for the image url.

The code will depend on which JSON parsing library you use, but the basics should be the same.

First you want to take the key values like 123,112,189 so we will take the keys into an array

say the structure like [ Web { 123 {image url} 112 {image url} 189 {image url} ]

so

 NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

 SBJSON *jsonParser = [SBJSON alloc]init];

 NSMutableArray *yourArray1 = [jsonParser objectWithString:responseString]copy]]autorelease;

    ufArray = [[yourArray1 valueForKey:@"web"] copy];

   for (NSString *s in ufArray) {

    [keys addObject:[NSDictionary dictionaryWithObjectsAndKeys:s,@"keys",nil]];
}

 NSLOG(@"keys :%@",keys);

 // this will contain 112,123,114 etc values
      initialize a NSMutableArray

      finalArray = [NSMutableArray alloc]init];

    for (int i = 0; i < [ufArray count]; i ++) {

    yourArray1 = [ufArray valueForKey:[[keys objectAtIndex:i]valueForKey:@"keys"]];

    // [keys object at indes:i] - > 123 val / next loop 112 array like that

    [finalArray addObject:yourArray1];
}


   [jsonParser release];

    jsonParser = nil;   

Hope this helps!

Well if that array was called jArray

var img_urls = [];
var jL = jArray[0][123].length;
var img_urls = [];
for(var i = 0; i < jL; i++){
    img_urls[i] = jArray[0][123][i].image_url;
}

//display in console: 
console.log(img_urls);

demo: http://jsfiddle.net/maniator/Vx3hu/4/

I've never used JSON before, never used iPhone before, never used Xcode before...but I would think its something along along the lines of...

//object and image for item ID 222
123: item_id(222).image_url("some_url")

or the second and following items

//hi
123: item_id(333).image_url("some_url")

However something better would be when you can extract the image without the URL by using the item ID and an image ID, so when calling the object 123, you can specify the item id and the image id, which would then output all the information you require. For instance the count, file type and the image could all be displayed.

123: item_id(222).image_id(222)

Is the data file SQL or XML? XML is usually faster! So read up on nodes.

Hope that helps.

DL.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top