简体   繁体   English

的IOS目标C解析JSON

[英]ios objective c parsing json

Trying to parse this json and cannot seem to figure it out. 尝试解析此json,似乎无法弄清楚。

{ description = "Description Variant 1 "; {description =“ Description Variant 1”; id = 4; id = 4; price = "25.0"; 价格=“25.0”; }, { description = "Variant 2 Description "; },{description =“Variant 2 Description”; id = 5; id = 5; price = "50.0"; 价格=“50.0”; }, { description = "Variant 3 Description"; },{description =“Variant 3 Description”; id = 6; id = 6; price = "75.0"; 价格=“75.0”; } }

Here is my code, but I get a SigAbt on the NSLog: 这是我的代码,但我在NSLog上获得了一个SigAbt:

- (NSMutableArray *) getVariants:(NSString *)variantJson 
{
    NSMutableArray *variants = [[NSMutableArray alloc] init];
    NSLog(@"Variant JSON: %@", variantJson);

    NSArray *vars = [variantJson valueForKeyPath:@"variants"];

    for (id var in vars) 
    {
        NSLog(@"description: %@",[var objectForKey:@"description"]);

    }

    return variants;
}

The json coming in to variable: variantJson is the above posted JSON. 传入变量json的变量:variantJson是上面发布的JSON。

iOS doesn't parse JSON this transparently; iOS不会透明地解析JSON。 you need to run your string through an actual JSON parser library, like SBJson . 您需要通过实际的JSON解析器库(例如SBJson)运行字符串。 (BSD-licensed) Or you can use the built-in NSJSONSerialization if you're targeting OS 5 or later. (获得BSD许可)或者,如果您的目标操作系统是OS 5或更高版本,则可以使用内置的NSJSONSerialization

You have no code for parsing the JSON there. 您没有用于解析JSON的代码。 Objective-C and Cocoa have not built-in mechanism for automagically parsing JSON string into objects and dictionaries and valueForKeyPath is for getting property value (within hierarchy) of KVC-compliant objective-c classes. Objective-C和Cocoa没有用于自动将JSON字符串解析为对象和字典的内置机制,而valueForKeyPath用于获取符合KVC的objective-c类的属性值(在层次结构内)。

In order to get nested NSDictionary 'ies and NSArray 's you need to employ some third party library or write your own code. 为了获得嵌套的NSDictionaryNSArray ,您需要使用一些第三方库或编写自己的代码。 Take a look at the list of libraries at JSON page . 查看JSON页面上的库列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM