简体   繁体   中英

Error While Parsing JSON String Response With Objective-C

The Response I Get :

{"response":{"id":"R1","cmd":[{"batchSize":50,"startRow":0,"name":"doLogin","result":"OK","attributes":[{"name":"businessName","type":"String"},{"name":"objId","type":"Long"},{"name":"businessType","type":"String"},{"name":"firstName","type":"String"},{"name":"businessName","type":"String"},{"name":"objId","type":"Long"},{"name":"businessType","type":"String"},{"name":"firstName","type":"String"}],"records":[["businessName\\":\\"Palo Alto Egg - Distributor","objId\\":\\"200","businessType\\":\\"D","firstName\\":\\"System"],["businessName\\":\\"Palo Alto Egg - Distributor","objId\\":\\"200","businessType\\":\\"D","firstName\\":\\"System"],["businessName\\":\\"Palo Alto Egg - Distributor","objId\\":\\"200","businessType\\":\\"D","firstName\\":\\"System"],["businessName\\":\\"Palo Alto Egg - Distributor","objId\\":\\"200","businessType\\":\\"D","firstName\\":\\"System"]]}]}}

Now The Problem IS That....... NSDictionary *json = [NSJSONSerialization JSONObjectWithData:(NSData *)obj options:kNilOptions error:&error];

NSDictionary *first = [json objectForKey:@"response"];
NSArray *second = [first objectForKey:@"cmd"];
NSArray *attribute_array = [[second objectAtIndex:0] objectForKey:@"result"];
NSLog(@"Resultttttttttt=%@",attribute_array);

//Value of Result
NSString *resultVal = [NSString stringWithFormat:@"%@",attribute_array];

NSArray *record_array = [[second objectAtIndex:0] objectForKey:@"records"];

NSLog(@"Resultttttttttt Nisarg = %@",[[record_array objectAtIndex:0] objectForKey:@"firstName\\"]);

In the last sentence when I'm trying to fetch the value for the key firstName it gives Error because the structure is like "firstName\\" instead of key "firstName" so any suggestion to parse the string with "firstName\\" Key....

Your JSON is well formed but each record is a list of strings, instead of a dictionary, ie you have

[
    "businessName\":\"Palo Alto Egg - Distributor",
    "objId\":\"200",
    "businessType\":\"D",
    "firstName\":\"System"
],

instead of

{
    "businessName": "Palo Alto Egg - Distributor",
    "objId": "200",
    "businessType": "D",
    "firstName": "System"
},

therefore [record_array objectAtIndex:0] is an array, not a dictionary.

If you have control over the JSON output, you should check the code and make it return the right format. If you cannot change the output, and you don't want to make complex string manipulations in the raw JSON data, your best alternative is the following:

NSString *firstName = NULL;

for (NSString *line in record) {
    if ([line hasPrefix:@"firstName"]) {
        firstName = [[line componentsSeparatedByString:@"\":\""] objectAtIndex:1];
    }
}

NSLog(@"%@", firstName);

Why escaping quotes?:

[["businessName\":\"Palo Alto Egg - Distributor","objId\":\"200","businessType\":\"D","firstName\":\"System"],["businessName\":\"Palo Alto Egg - Distributor","objId\":\"200","businessType\":\"D","firstName\":\"System"],["businessName\":\"Palo Alto Egg - Distributor","objId\":\"200","businessType\":\"D","firstName\":\"System"]

It must be:

[["businessName":"Palo Alto Egg - Distributor","objId":"200","businessType":"D","firstName":"System"],["businessName":"Palo Alto Egg - Distributor","objId":"200","businessType":"D","firstName":"System"],["businessName":"Palo Alto Egg - Distributor","objId":"200","businessType":"D","firstName":"System"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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