将键值对附加到我的 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'
如何将标题标签附加到每个数据的第一项。一些类似的东西会让我区分这些是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想向 Javascript 对象添加属性(而不是操作 JSON 字符串),这非常简单。无论您想在哪里添加 '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: