考虑从.NET Web服务返回的以下JSON(根据jsonlint有效):
{"d":"{\"categories\":{\"category\":[{\"categoryID\":\"edNXeLBcDEa6sAFrxmjoXw\",\"categoryName\":\"Academics\",\"webSiteID\":\"jXKvJ3R85kiAodlhw06FjQ\",\"websiteName\":\"MySite\",\"departmentID\":\"FU0tEh2MCEu54qiVgBX6Yg\",\"departmentName\":\"WebServices\",\"departmentWebSite\":null,\"departmentLogo\":null},{\"categoryID\":\"9QcMMZjMo0G7kMM3kP7Apw\",\"categoryName\":\"Admissions\",\"webSiteID\":\"jXKvJ3R85kiAodlhw06FjQ\",\"websiteName\":\"MySite\",\"departmentID\":\"FU0tEh2MCEu54qiVgBX6Yg\",\"departmentName\":\"WebServices\",\"departmentWebSite\":null,\"departmentLogo\":null},{\"categoryID\":\"Tm207w31ZE6c39DJ1Juk2Q\",\"categoryName\":\"Webinars\",\"webSiteID\":\"jXKvJ3R85kiAodlhw06FjQ\",\"websiteName\":\"MySite\",\"departmentID\":\"FU0tEh2MCEu54qiVgBX6Yg\",\"departmentName\":\"WebServices\",\"departmentWebSite\":null,\"departmentLogo\":null}],\"metaData\":{\"recordCount\":\"3\",\"pageIndex\":\"0\",\"pageSize\":\"0\"}}}"}
使用序列化JSON执行以下操作可以正常工作:
NSDictionary *d = [jsonResults objectForKey:@"d"];
这使得“d”的值:
{"categories":{"category":[{"categoryID":"edNXeLBcDEa6sAFrxmjoXw","categoryName":"Academics","webSiteID":"jXKvJ3R85kiAodlhw06FjQ","websiteName":"MySite","departmentID":"FU0tEh2MCEu54qiVgBX6Yg","departmentName":"WebServices","departmentWebSite":null,"departmentLogo":null},{"categoryID":"9QcMMZjMo0G7kMM3kP7Apw","categoryName":"Admissions","webSiteID":"jXKvJ3R85kiAodlhw06FjQ","websiteName":"MySite","departmentID":"FU0tEh2MCEu54qiVgBX6Yg","departmentName":"WebServices","departmentWebSite":null,"departmentLogo":null},{"categoryID":"Tm207w31ZE6c39DJ1Juk2Q","categoryName":"Webinars","webSiteID":"jXKvJ3R85kiAodlhw06FjQ","websiteName":"MySite","departmentID":"FU0tEh2MCEu54qiVgBX6Yg","departmentName":"WebServices","departmentWebSite":null,"departmentLogo":null}],"metaData":{"recordCount":"3","pageIndex":"0","pageSize":"0"}}}
如果我尝试调用“类别”的objectForKey,我只会收到“无法识别的选择器发送到实例”错误:
NSDictionary *categories = [d objectForKey:@"categories"];
我已经尝试了我能想到的一切,但我永远不会在这个JSON中获得超过一层的深度。我能用Java完成这项工作,但我正在尝试编写我的应用程序的iOS版本。由于我是Objective C的新手,我一定是缺少一些东西。有什么想法吗?
修改
这就是我在Java中用这个相同的JSON做的事情,如果这有帮助:
JSONArray arrCategories = null;
String strJson = JSON.postJSONData("http://mysite.com/webservice.asmx/GetCategories", "{'destinationId' : '" + DestinationID + "', 'orderBy' : 'categoryName', 'orderByDirection' : 'asc'}");
String d = new JSONObject(strJson).getString("d");
String strCategories = new JSONObject(d).getString("categories");
arrCategories = new JSONObject(strCategories).getJSONArray("category");
for(int i=0; i < arrCategories.length(); i++) {
JSONObject objCategory = new JSONObject(arrCategories.getString(i));
String categoryID = objCategory.getString("categoryID");
String categoryName = objCategory.getString("categoryName");
}
答案 0 :(得分:0)
您没有AJAX字符串。嗯,你这样做,但问题是,你里面有一个字符串。
d键以与以下相同的方式映射到字符串:
{name:“blah”}会将obj.name映射到字符串“blah”。
基本上,你需要解析JSON,然后在JSON中解析JSON。