1

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....

1
  • Show the JSON dict after deserialization of response. Commented Jan 17, 2014 at 14:42

2 Answers 2

1

Your JSON is well formed but each record is a list of strings, instead of a dictionary, i.e. 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);
Sign up to request clarification or add additional context in comments.

Comments

0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.