【问题标题】:Error While Parsing JSON String Response With Objective-C使用 Objective-C 解析 JSON 字符串响应时出错
【发布时间】:2014-01-17 13:26:40
【问题描述】:

我得到的回应:

{"response":{"id":"R1","cmd":[{"batchSize":50,"startRow":0,"name":"doLogin","re​​sult":"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"}] ,"记录":[["企业名称\":\"帕洛 中音蛋 - 分销商","objId\":\"200","businessType\":\"D","firstName\":\"System"],["businessName\":\"Palo 中音蛋 - 分销商","objId\":\"200","businessType\":\"D","firstName\":\"System"],["businessName\":\"Palo 中音蛋 - 分销商","objId\":\"200","businessType\":\"D","firstName\":\"System"],["businessName\":\"Palo 中音蛋 - 分销商","objId\":\"200","businessType\":\"D","firstName\":\"System"]]}]}}

现在的问题是...... 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\\"]);

在最后一句中,当我尝试获取键 firstName 的值时,它会给出错误,因为结构类似于 "firstName\" 而不是键 "firstName" 所以任何用 "firstName\" Key....

解析字符串的建议

【问题讨论】:

  • 响应反序列化后显示 JSON 字典。

标签: ios objective-c json


【解决方案1】:

您的 JSON 格式正确,但每条记录都是字符串列表,而不是字典,即您有

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

而不是

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

因此[record_array objectAtIndex:0] 是一个数组,而不是字典。

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

NSString *firstName = NULL;

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

NSLog(@"%@", firstName);

【讨论】:

    【解决方案2】:

    为什么要转义引号?:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 2012-10-04
      相关资源
      最近更新 更多