2011-05-06 56 views
0

這是我的JSON響應追加鍵,值對我的JSON數據

{ 
    "PatientSearchResult": { 
     "Patient": [{ 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": null, 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "2914 FirstName" 
     }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": "\/Date(378709200000-0500)\/", 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "4261 FirstName" 
      }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": "\/Date(1136264400000-0500)\/", 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "Bo" 
      }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": "\/Date(-2192126400000-0400)\/", 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "Mark" 
      }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": "\/Date(-381697200000-0500)\/", 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "Scott" 
      }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": "\/Date(-445806000000-0500)\/", 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "John" 
      }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": "\/Date(-880830000000-0500)\/", 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "Dagwood"     
     }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": null, 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "Clyde" 
      }, { 
      "AccountBalanceCalcMethod": 2, 
      "AlternatePatientID": 0, 
      "AssignmentOfBenifits": 0, 
      "CellPhoneNumber1": null, 
      "CellPhoneNumber2": null, 
      "Citizenship": 0, 
      "Credential": 0, 
      "DateOfBirth": "\/Date(-249678000000-0500)\/", 
      "Deceased": 0, 
      "DeceasedDate": "\/Date(-62135578800000-0500)\/", 
      "DoesPatientHaveResidentProof": false, 
      "DriversLicenseNumber": null, 
      "DriversLicenseState": 0, 
      "EmailAddress1": null, 
      "EmailAddress2": null, 
      "Enabled": false, 
      "Ethnicity": 0, 
      "Firstname": "Fred" 
      }] 
    } 
} 

我試圖解析它這樣。

function SortByName(x,y) {   
    return ((x.LastName == y.LastName) ? 
     0 : ((x.LastName > y.LastName) ? 1 : -1));  
} 

function RenderPatientSearchData(PatientSearchResponse){ 
    var PatientSearchData = JSON.parse(PatientSearchResponse); 
    var results = PatientSearchData['PatientSearchResult']; 
    results.Patient.sort(SortByName); 
    for (i = 0, len = results.PatientSearchResult.Patient.length; i < len; i++) { 
     // some code here 
    } 
    //.... 
} 

如何將鍵值對添加到我的JSON中。我想追加

Header:'somevalue' 

我該如何將標頭標籤附加到每個數據的第一項。有的像這將使我區分,這些都是一個名稱,這爲B名稱...

你可以看到數據在這個環節上的樣本響應......像這樣

https://github.com/appcelerator/KitchenSink/raw/master/Resources/examples/table_view_headers.js

+1

這不是JSON,它只是一個JavaScript對象。 – 2011-05-06 13:05:42

+1

更確切地說:第一個是JSON,但PatientSearchData是JS對象。那麼你想改變JSON還是添加屬性到'PatientSearchData'? – 2011-05-06 13:08:48

+0

添加屬性到我的PatientSearchData ...我正在使用結果對象...所以它的一個JS對象 – 2011-05-06 13:14:06

回答

3

這很簡單,如果你想添加一個Javascript對象的屬性(而不是操縱JSON字符串)。無論你想添加'標題':'somevalue'只需要:

PatientSearchData.Header = 'somevalue'; 
+0

我如何將它添加到某些對象..和某些對象不應該包含標頭 – 2011-05-06 13:20:58

+0

@約翰 - 它可能是像' PatientSearchData.Patient [0] .Header ='somevalue';'把它當作普通的JS對象。 [本文](http://www.quirksmode.org/js/associative.html)將JS對象作爲關聯數組應該有助於您更好地理解整個主題。 – justkt 2011-05-06 13:22:44

+0

但是我怎樣才能添加這個...新的頭...和哪裏..我很困惑 – 2011-05-06 13:28:55