将键,值对附加到我的JSON数据

时间:2011-05-06 13:03:38

标签: javascript 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
    }
    //....
}

如何将key,value对附加到我的JSON。我想追加

Header:'somevalue'

如何将Header标签附加到每个数据的第一项。有些会让我区分出这些是A名,这些是B名......

您可以在此链接中看到数据的示例响应......类似这样

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

1 个答案:

答案 0 :(得分:3)

如果要向Javascript对象添加属性(而不是操作JSON字符串),这非常简单。无论你想在哪里添加'标题':'somevalue'只需:

PatientSearchData.Header = 'somevalue';