如何从此json中获取数据
Posted
技术标签:
【中文标题】如何从此json中获取数据【英文标题】:How to fetch data from this json 【发布时间】:2011-12-18 11:22:08 【问题描述】:我希望这些数据显示在 tableview 中。谁能告诉我如何在字典或数组中获取数据并显示在表格中?数据如下所示
我想显示city
和custno
"Status": 200,
"data": [
"dispatch": "11111",
"locname": "Sample Company Location 1",
"add1": "226 Engleman Street",
"city": "Bardstown",
"state": "KY",
"zip": "40004",
"contacts": [
"id": "1",
"custno": "0000235",
"locno": "00001",
"salutation": "Mr.",
"jobtitle": "IT Manager",
"locktime": "",
"lockby": ""
,
"id": "11",
"custno": "0000235",
"locno": "00001",
"contact": "Greg C",
"extension": "205",
"salutation": "Mr. ",
"jobtitle": "President",
"locktime": null,
"lockby": null
,
"id": "12",
"custno": "0000235",
"locno": "00001",
"extension": "206",
"salutation": "Mr.",
"jobtitle": "Parts Manager",
"locktime": null,
"lockby": null
,
"id": "13",
"custno": "0000235",
"locno": "00001",
"extension": "202",
"salutation": "Mr.",
"jobtitle": "Service Manager",
"locktime": "",
"lockby": ""
],
"locationnotes": "5/20/2010 9:32:06 PM - Updated Information1111111",
"custno": "0000235",
"locno": "00001",
"calledinby": "Teresa",
"serviceagrno": "",
"dispatchnotes": "This is a test dispatch",
"pastcustomernotes": "",
"pasttechnotes": [],
"status": "Pending",
"locationimage": "/images/content/2t_1318687848_100x100.gif",
"pmstatus": "Yes"
,
"dispatch": "4",
"locname": "Sample Company Location 1",
"add1": "226 Engleman Street",
"city": "Bardstown",
"state": "KY",
"zip": "40004",
"contacts": [
"id": "1",
"custno": "0000235",
"locno": "00001",
"extension": "200",
"salutation": "Mr.",
"jobtitle": "IT Manager",
"locktime": "",
"lockby": ""
,
"id": "11",
"custno": "0000235",
"locno": "00001",
"phonefax": "555-555-1002",
"extension": "205",
"salutation": "Mr. ",
"jobtitle": "President",
"locktime": null,
"lockby": null
,
"id": "12",
"custno": "0000235",
"locno": "00001",
"extension": "206",
"salutation": "Mr.",
"jobtitle": "Parts Manager",
"locktime": null,
"lockby": null
,
"id": "13",
"custno": "0000235",
"locno": "00001",
"extension": "202",
"salutation": "Mr.",
"jobtitle": "Service Manager",
"locktime": "",
"lockby": ""
],
"locationnotes": "5/20/2010 9:32:06 PM - Updated Information1111111",
"custno": "0000235",
"locno": "00001",
"calledinby": "Teresa",
"serviceagrno": null,
"dispatchnotes": "11/3/2011 7:59 - This is a sample ATM call.",
"pastcustomernotes": "",
"pasttechnotes": [],
"status": "Pending",
"locationimage": "/images/content/2t_1318687848_100x100.gif",
"pmstatus": "No"
,
"dispatch": "5",
"locname": "Sample Company Location 1",
"add1": "226 Engleman Street",
"city": "Bardstown",
"state": "KY",
"zip": "40004",
"contacts": [
"id": "1",
"custno": "0000235",
"locno": "00001",
"contact": "Michael B",
"email": "michael@bonzahouse.com",
"phoneoffice": "502-555-1000",
"phonemobile": "502-555-0769",
"phonefax": "555-555-1002",
"extension": "200",
"salutation": "Mr.",
"jobtitle": "IT Manager",
"locktime": "",
"lockby": ""
,
"id": "11",
"custno": "0000235",
"locno": "00001",
"extension": "205",
"salutation": "Mr. ",
"jobtitle": "President",
"locktime": null,
"lockby": null
,
"id": "12",
"custno": "0000235",
"locno": "00001",
"extension": "206",
"salutation": "Mr.",
"jobtitle": "Parts Manager",
"locktime": null,
"lockby": null
,
"id": "13",
"custno": "0000235",
"locno": "00001",
"extension": "202",
"salutation": "Mr.",
"jobtitle": "Service Manager",
"locktime": "",
"lockby": ""
],
"locationnotes": "5/20/2010 9:32:06 PM - Updated Information1111111",
"custno": "0000235",
"locno": "00001",
"calledinby": "",
"serviceagrno": null,
"dispatchnotes": "",
"pastcustomernotes": "",
"pasttechnotes": [],
"status": "Pending",
"locationimage": "/images/content/2t_1318687848_100x100.gif",
"pmstatus": "Yes"
]
【问题讨论】:
【参考方案1】:您需要使用 NSJSONSerialization
(仅适用于 ios 5 及更高版本)或其他 JSON 框架(我推荐 TouchJSON - https://github.com/TouchCode/TouchJSON)将其读入 NSDictionary
。
以下是示例:
NSJSON序列化:
NSString *jsonString = <JSON_STRING>;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:&error];
TouchJSON:
NSString *jsonString = <JSON_STRING>;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *dictionary = [[CJSONDeserializer deserializer]
deserializeAsDictionary:jsonData
error:&error];
一旦你有了字典,你就可以提取你想要的信息。在您的情况下,您似乎想要获取 data
这是一个对象数组。我假设您想在表格中显示有关这些对象的一些信息。因此,这是该代码的开始,可以让您继续前进,然后您应该能够弄清楚其余部分:
NSArray *data = [dictionary objectForKey:@"data"];
// Then for each data object, you can get at say its `locname' attribute to show in a cell like so
NSString *locname = [[data objectAtIndex:i] objectForKey:@"locname"];
【讨论】:
使用 NSJSONSerialization 时,应该使用“options:kNilOptions”而不是“options:0”。两者都有效,但在我看来,前者似乎更具可读性(并且是 Apple 推荐的)。以上是关于如何从此json中获取数据的主要内容,如果未能解决你的问题,请参考以下文章
Android:如何从此 json 获取 JSON 对象键: