Javascript 将键、值对附加到我的JSON数据
这是我的JSON响应Javascript 将键、值对附加到我的JSON数据,javascript,json,Javascript,Json,这是我的JSON响应 { "PatientSearchResult": { "Patient": [{ "AccountBalanceCalcMethod": 2, "AlternatePatientID": 0, "AssignmentOfBenifits": 0, "CellPhoneNumber1": null, "CellPhoneNumber2
{
"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
}
//....
}
如何将标题标签附加到每个数据的第一项。一些类似的会让我区分这些是A名字,这些是B名字
您可以在此链接中看到数据的示例响应。。。像这样的
如果您想向Javascript对象添加属性(而不是操作JSON字符串),这非常简单。无论您想在何处添加“Header”:“somevalue”,只需执行以下操作:
PatientSearchData.Header = 'somevalue';
如果您想向Javascript对象添加属性(而不是操作JSON字符串),这非常简单。无论您想在何处添加“Header”:“somevalue”,只需执行以下操作:
PatientSearchData.Header = 'somevalue';
那不是JSON,那只是一个JavaScript对象。更准确地说:第一个是JSON,但是
PatientSearchData
是一个JS对象。那么,您想更改JSON还是将属性添加到PatientSearchData
?将属性添加到我的PatientSearchData。。。我正在使用结果对象。。。所以它是一个JS对象,不是JSON,只是一个JavaScript对象。更准确地说:第一个是JSON,但是PatientSearchData
是一个JS对象。那么,您想更改JSON还是将属性添加到PatientSearchData
?将属性添加到我的PatientSearchData。。。我正在使用结果对象。。。因此,它是一个JS对象如何将其添加到某些对象中。。并且某些对象不应该包含Header@John-它可能类似于PatientSearchData.Patient[0]。Header='somevalue'代码>只需将其视为普通JS对象即可。在JS对象上作为关联数组应该可以帮助您更好地理解整个主题。。。新标题。。。在哪里。。我是confused@John-你想把它添加到哪里?如果不知道对象头是什么属性,我就无法告诉您它的位置。它不像newheader()
那样工作,除非您创建了Header()
对象。你熟悉Javascript版本的面向对象吗?我想把它添加到某些属性中,这个标题是:'A'来分组我的Json字符串。。。或者我如何创建一个新的属性头:'a'并在解析后将其添加到我的对象中我如何才能将其添加到某些对象中。。并且某些对象不应该包含Header@John-它可能类似于PatientSearchData.Patient[0]。Header='somevalue'代码>只需将其视为普通JS对象即可。在JS对象上作为关联数组应该可以帮助您更好地理解整个主题。。。新标题。。。在哪里。。我是confused@John-你想把它添加到哪里?如果不知道对象头是什么属性,我就无法告诉您它的位置。它不像newheader()
那样工作,除非您创建了Header()
对象。你熟悉Javascript版本的面向对象吗?我想把它添加到某些属性中,这个标题是:'A'来分组我的Json字符串。。。或者我如何创建一个新的属性头:'a',并在解析后将其添加到我的对象中