Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Objective-C解析JSON字符串响应时出错_Ios_Objective C_Json - Fatal编程技术网

Ios 使用Objective-C解析JSON字符串响应时出错

Ios 使用Objective-C解析JSON字符串响应时出错,ios,objective-c,json,Ios,Objective C,Json,我得到的答复是: {“response”:{“id”:“R1”,“cmd”:[{“batchSize”:50,“startRow”:0,“name”:“doLogin”,“result”:“OK”,“attributes”:[{“name”:“businessName”,“type”:“String”},{“name”:“objId”,“type”:“Long”},{“name”;“firstName”,“type”:“String”},{“name”:“businessName”,“type”

我得到的答复是:

{“response”:{“id”:“R1”,“cmd”:[{“batchSize”:50,“startRow”:0,“name”:“doLogin”,“result”:“OK”,“attributes”:[{“name”:“businessName”,“type”:“String”},{“name”:“objId”,“type”:“Long”},{“name”;“firstName”,“type”:“String”},{“name”:“businessName”,“type”:“String”},{“name”;“name”;“type”;“String”;{“name”;“name”;“objId”,“type”;“type”:“Long”;“name”;{“name”;“businessType”;“businessType”,“类型”:“String”},{“name”:“firstName”,“type”:“String”}],“records”:[[“businessName\”:\“Palo” 中音蛋- 分销商“,”objId\“:”200“,”业务类型\“:”D“,”名字\“:\”系统“],[”业务名称\“:\”帕洛 中音蛋- 分销商“,”objId\“:”200“,”业务类型\“:”D“,”名字\“:\”系统“],[”业务名称\“:\”帕洛 中音蛋- 分销商“,”objId\“:”200“,”业务类型\“:”D“,”名字\“:\”系统“],[”业务名称\“:\”帕洛 中音蛋- 分销商“,”对象\“:\”200“,”业务类型\“:\”D“,”名字\“:\”系统]]}}

现在的问题是…… NSDictionary*json=[NSJSONSerialization JSONObjectWithData:(NSData*)对象选项:针织错误:&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\\"]);
在最后一句话中,当我试图获取键firstName的值时,它给出了错误,因为结构类似于“firstName\”,而不是键“firstName”,所以有没有建议使用“firstName\”键解析字符串…

为什么要转义引号

[["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"],["businessName":"Palo Alto Egg - Distributor","objId":"200","businessType":"D","firstName":"System"],["businessName":"Palo Alto Egg - Distributor","objId":"200","businessType":"D","firstName":"System"]

JSON格式良好,但每条记录都是字符串列表,而不是字典,即

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

{
    "businessName": "Palo Alto Egg - Distributor",
    "objId": "200",
    "businessType": "D",
    "firstName": "System"
},
因此,
[record\u array objectAtIndex:0]
是一个数组,而不是字典

如果您可以控制JSON输出,那么应该检查代码并使其返回正确的格式。 如果无法更改输出,并且不想对原始JSON数据进行复杂的字符串操作,那么最好的选择是:

NSString *firstName = NULL;

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

NSLog(@"%@", firstName);

在反序列化响应后显示JSON dict。