【问题标题】:parsing JSON object and sub-elements on iphone app在 iphone 应用程序上解析 JSON 对象和子元素
【发布时间】:2011-11-01 23:37:40
【问题描述】:

我正在构建一个使用 UPC 数据库 API 的应用。我正在取回一个 JSON 对象,例如:http://www.simpleupc.com/api/methods/FetchNutritionFactsByUPC.php

{
"success":true,
"usedExternal":false,
"result"
    {
        "calories_per_serving":"150",
        "cholesterol_per_serving":"15",
        "cholesterol_uom":"Mg",
        "dvp_calcium":"30",
        "dvp_cholesterol":"4",
        "dvp_iron":"2",
        "dvp_protein":"17",
        "dvp_saturated_fat":"8",
        "dvp_sodium":"10",
        "dvp_total_fat":"4",
        "dvp_vitamin_a":"10","
        "dvp_vitamin_c":"0",
        "dvp_vitamin_d":"25",
        "fat_calories_per_serving":"25",
        "fiber_per_serving":"<1",
        "fiber_uom":"G",
        "ingredients":"Fat Free Milk, Milk, Sugar, Cocoa (Processed With Alkali),
                       Salt, Carrageenan, Vanillin (Artificial Flavor),
                       Lactase Enzyme, Vitamin A Palmitate And Vitamin D3.",
        "protein_per_serving":"8",
        "protein_uom":"G",
        "size":"240",
        "units":"mL",
        "servings_per_container":"8",
        "sodium_per_serving":"230",
        "sodium_uom":"Mg",
        "total_fat_per_serving":"2.5",
        "total_fat_uom":"G",
        "trans_fat_per_serving":"0",
        "trans_fat_uom":"G",
        "upc":"041383096013"
    }
}

我的问题是解析“ingredients”元素,它是对象字典的子列表。

您建议如何解析成分列表?如果我可以把它放到一个 NSArray 中,假设逗号是分隔符,那就太好了。

我试图这样做,但看起来它只是一个字符串,所以无法解析它。

任何建议都会受到欢迎。谢谢!

   //Thats the whole JSON object
    NSDictionary *json_dict = [theResponseString JSONValue];


   //Getting "results" which has all the product info
    NSArray *myArray = [[NSArray alloc] init];
     myArray = [json_dict valueForKey:@"result"];

现在我如何以数组形式从 myArray 中获取“成分”?

【问题讨论】:

    标签: iphone ios arrays xcode json


    【解决方案1】:

    您将result 作为一个数组获取,但(在 JSON 术语中)它不是一个数组。 It's an object, so use an NSDictionary。像这样:1

    NSDictionary *result = [json_dict objectForKey:@"result"];
    

    然后您可以从中获取内部ingredients 对象:

    NSString *ingredients = [result objectForKey:@"ingredients"];
    

    已根据@Bavarious 的评论编辑


    1对于明显的错误,我深表歉意,因为我对 Objective-C 不是很精通。您可能需要为返回的NSDictionaryNSString 指针分配内存;我不确定。

    【讨论】:

    • 谢谢,但我需要数组形式的成分,这样我就可以访问每种成分。
    • 这一切都很好。不幸的是,出于您的目的,您坚持使用 JSON 中的格式。这意味着您首先必须将成分作为字符串检索,然后将该字符串拆分为 ,(逗号)到一个数组中。我同意 JSON 格式是愚蠢的,但必须玩你的手。或者,你知道,使用不同的 API。
    • 我建议使用-objectForKey:,规范的NSDictionary 方法从字典中检索元素,而不是-valueForKey:。见stackoverflow.com/questions/4489684/…
    • @Bavarious 谢谢,已相应编辑。就像我说的:我不太了解 Objective-C。
    【解决方案2】:

    这就是你需要做的所有事情:

     NSDictionary *json_dict = [theResponseString JSONValue];
    
     // Use a key path to access the nested element.
     NSArray *myArray = [json_dict valueForKeyPath:@"result.ingredients"];
    

    编辑

    哎呀,马特是对的。下面是处理字符串值的方法:

     // Use a key path to access the nested element.
     NSString *s = [json_dict valueForKeyPath:@"result.ingredients"];
     NSArray *ingredients = [s componentsSeparatedByString:@", "];
    

    请注意,您可能需要修剪 '.'数组最后一个元素的字符。

    【讨论】:

    • 仔细查看 JSON。 result.ingredients 不是数组。这是一个字符串。
    • 不错的尝试 - 本来可以很干净,但 myArray 在这种情况下不是数组 - 它是一个字符串(同样,在它上面运行“count”会使应用程序崩溃)。
    • Matt 是对的——它是一个字符串,所以看起来必须蛮力。
    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多