2011-12-18 220 views
0

我希望將這些數據顯示在tableview中。誰能告訴我如何在字典或數組中獲取數據並在表中顯示?該數據被示出爲下如何從此json獲取數據

我想顯示citycustno

{ 
"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": "[email protected]", 
       "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

您需要讀取到NSDictionary與任一NSJSONSerialization(僅在IOS 5和以後版本)或另一個JSON框架(我推薦TouchJSON - https://github.com/TouchCode/TouchJSON)。

下面舉例說明:

NSJSONSerialization:

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"]; 
+0

感謝........ ,,,,, – 2011-12-18 11:50:49

+0

當使用NSJSONSerialization,一個應可能使用「選項:kNilOptions」而不是「選項:0」。兩者都可以工作,但前者看起來更具可讀性(並且是Apple推薦的)。 – 2012-04-05 21:01:18