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

发布于 2024-11-05 15:17:57 字数 7406 浏览 0 评论 0原文

这是我的 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'

如何将标题标签附加到每个数据的第一项。一些类似的东西会让我区分这些是 A 名称,这些是 B 名称...

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

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

This is my JSON response

{
    "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"
            }]
    }
}

I am trying to parse it like this.

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
    }
    //....
}

How can i append key,value pair to my JSON. I want to append

Header:'somevalue'

how would i append a Header label to first items of each data. some like which will make me differentiate that these are A Names , these are B Names...

You can see a sample response of the data in this link... something like this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-11-12 15:17:57

如果您想向 Javascript 对象添加属性(而不是操作 JSON 字符串),这非常简单。无论您想在哪里添加 'Header': 'somevalue' ,只需执行以下操作:

PatientSearchData.Header = 'somevalue';

It's pretty simple if you want to add properties to a Javascript object (as opposed to manipulating a JSON string). Wherever you want to add 'Header': 'somevalue' just do:

PatientSearchData.Header = 'somevalue';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文