如何从ios中的数组中获取json

时间:2013-02-01 12:21:13

标签: objective-c nsarray

我有一个包含字符串的数组,而json有对象,我需要从数组中获取json。如何从数组中获取json?我在下面展示了数组:

array (
    "",
    "",
    "",
    "sda@sdas.com",
    "[{  \"field_value\" : \"\\/Users\\/poornima\\/Library\\/Application Support\\/iPhone Simulator\\/5.1\\/Applications\\/BCA9D949-D85D-4CA7-B753-7315C0383137\\/Documents\\/EGOoTtqT4e.png\",  \"field_name\" : \"Photo\"},{  \"field_value\" : \"\",  \"field_name\" : \"Signature\"},{  \"field_value\" : \"\",  \"field_name\" : \"File Upload\"}]",
    "",
    "",
    "",
    "37.785834",
    "-122.406417",
    "2013-02-01 05:44:01",
    "superadmin@gmail.com",
    ""
)

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

看起来像这样..

NSString *jsonString = @"your json";
NSData *JSONdata = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError = nil;
if (JSONdata != nil) {
    //this you need to know json root is NSDictionary or NSArray , you smaple is NSDictionary
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:JSONdata options:0 error:&jsonError];
    if (jsonError == nil) {
        //every need check value is null or not , json null like ( "count": null )
        if (dic == (NSDictionary *)[NSNull null]) {
            return nil;
        }

        //every property you must know , what type is

        if ([dic objectForKey:@"count"] != [NSNull null]) {
            [self setCount:[[dic objectForKey:@"count"] integerValue]];
        }
        if ([dic objectForKey:@"item"] != [NSNull null]) {
            NSArray *itemArray = [dic objectForKey:@"item"]; // check null if need
            for (NSDictionary *itemDic in itemArray){
                NSString *_id = [dic objectForKey:@"id"]; // check null if need
                NSNumber *found = (NSNumber *)[dic objectForKey:@"found"];
                //.....
                //.... just Dictionary get key value
            }

        }
    }
}

答案 1 :(得分:0)

你必须这样做:

for(NSString * arrayName中的字符串) {
NSDictionary * dicReturn = [string JSONValue];          if(dicReturn!= nil){ NSString * fieldvalue =[dicReturn objectForKey:@"field_value"]; NSString *fileName =[dicReturn objectForKey:@"field_name"]; } }