【问题标题】:how to get the json from array in ios如何从ios中的数组中获取json
【发布时间】:2013-02-01 12:21:12
【问题描述】:

我有一个包含字符串的数组,而 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",
    ""
)

任何帮助将不胜感激

【问题讨论】:

  • 呃,引用数组元素? (记住,JSON 是一个字符串。)如果您想要 JSON 的对象表示,请通过 JSON 解析器发送它。
  • 或者您不知道如何阅读 Apple NSArray 文档以了解如何提取数组元素?

标签: objective-c nsarray


【解决方案1】:

看起来像这样..

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
            }

        }
    }
}

【讨论】:

    【解决方案2】:

    你必须这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 2020-07-23
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多