我有从SugarCRM API返回的这个看起来很糟糕的JSON。
清理它并删除“name”键的最佳方法是什么,只需要一个键和值(没有嵌套对象)
如果需要,我不介意使用下划线库。
{
"assigned_user_name": {
"name": "assigned_user_name",
"value": ""
},
"modified_by_name": {
"name": "modified_by_name",
"value": "Website Administrator"
},
"created_by_name": {
"name": "created_by_name",
"value": "Website Administrator"
},
"id": {
"name": "id",
"value": "6f9ec13f-dc29-ff18-da36-52d81a0076ad"
},
"name": {
"name": "name",
"value": " "
},
"date_entered": {
"name": "date_entered",
"value": "2014-01-16 17:45:33"
},
"date_modified": {
"name": "date_modified",
"value": "2014-01-16 17:45:33"
},
"modified_user_id": {
"name": "modified_user_id",
"value": "ab5ff74f-8043-f125-1409-523b6767fca9"
},
"created_by": {
"name": "created_by",
"value": "ab5ff74f-8043-f125-1409-523b6767fca9"
},
"description": {
"name": "description",
"value": ""
},
"deleted": {
"name": "deleted",
"value": "0"
},
"assigned_user_id": {
"name": "assigned_user_id",
"value": ""
},
"salutation": {
"name": "salutation",
"value": ""
},
"first_name": {
"name": "first_name",
"value": ""
},
"last_name": {
"name": "last_name",
"value": ""
},
"full_name": {
"name": "full_name",
"value": " "
},
"title": {
"name": "title",
"value": ""
},
"department": {
"name": "department",
"value": ""
},
"do_not_call": {
"name": "do_not_call",
"value": "0"
},
"phone_home": {
"name": "phone_home",
"value": ""
},
"email": {
"name": "email",
"value": ""
},
"phone_mobile": {
"name": "phone_mobile",
"value": ""
},
"phone_work": {
"name": "phone_work",
"value": ""
},
"phone_other": {
"name": "phone_other",
"value": ""
},
"phone_fax": {
"name": "phone_fax",
"value": ""
},
"email1": {
"name": "email1",
"value": "test@example.com"
},
"email2": {
"name": "email2",
"value": ""
},
"invalid_email": {
"name": "invalid_email",
"value": "0"
},
"email_opt_out": {
"name": "email_opt_out",
"value": "0"
},
"primary_address_street": {
"name": "primary_address_street",
"value": ""
},
"primary_address_street_2": {
"name": "primary_address_street_2",
"value": ""
},
"primary_address_street_3": {
"name": "primary_address_street_3",
"value": ""
},
"primary_address_city": {
"name": "primary_address_city",
"value": ""
},
"primary_address_state": {
"name": "primary_address_state",
"value": ""
},
"primary_address_postalcode": {
"name": "primary_address_postalcode",
"value": ""
},
"primary_address_country": {
"name": "primary_address_country",
"value": ""
},
"alt_address_street": {
"name": "alt_address_street",
"value": ""
},
"alt_address_street_2": {
"name": "alt_address_street_2",
"value": ""
},
"alt_address_street_3": {
"name": "alt_address_street_3",
"value": ""
},
"alt_address_city": {
"name": "alt_address_city",
"value": ""
},
"alt_address_state": {
"name": "alt_address_state",
"value": ""
},
"alt_address_postalcode": {
"name": "alt_address_postalcode",
"value": ""
},
"alt_address_country": {
"name": "alt_address_country",
"value": ""
},
"assistant": {
"name": "assistant",
"value": ""
},
"assistant_phone": {
"name": "assistant_phone",
"value": ""
},
"email_and_name1": {
"name": "email_and_name1",
"value": ""
},
"lead_source": {
"name": "lead_source",
"value": ""
},
"opportunity_role_fields": {
"name": "opportunity_role_fields",
"value": ""
},
"opportunity_role_id": {
"name": "opportunity_role_id",
"value": ""
},
"opportunity_role": {
"name": "opportunity_role",
"value": ""
},
"reports_to_id": {
"name": "reports_to_id",
"value": ""
},
"report_to_name": {
"name": "report_to_name",
"value": ""
},
"birthdate": {
"name": "birthdate",
"value": false
},
"campaign_id": {
"name": "campaign_id",
"value": ""
},
"campaign_name": {
"name": "campaign_name",
"value": ""
},
"c_accept_status_fields": {
"name": "c_accept_status_fields",
"value": ""
},
"m_accept_status_fields": {
"name": "m_accept_status_fields",
"value": ""
},
"accept_status_id": {
"name": "accept_status_id",
"value": ""
},
"accept_status_name": {
"name": "accept_status_name",
"value": ""
},
"sync_contact": {
"name": "sync_contact",
"value": ""
},
"event_contact_fields": {
"name": "event_contact_fields",
"value": ""
},
"event_contact_id": {
"name": "event_contact_id",
"value": ""
},
"event_status": {
"name": "event_status",
"value": ""
},
"cc_sync": {
"name": "cc_sync",
"value": "0"
},
"cc_id": {
"name": "cc_id",
"value": ""
},
"cc_lists": {
"name": "cc_lists",
"value": ""
},
"cc_lists_view": {
"name": "cc_lists_view",
"value": ""
},
"cc_optout": {
"name": "cc_optout",
"value": "0"
},
"mc_expires_c": {
"name": "mc_expires_c",
"value": false
},
"birthyear_c": {
"name": "birthyear_c",
"value": ""
},
"contact_id_c": {
"name": "contact_id_c",
"value": ""
},
"currency_id": {
"name": "currency_id",
"value": ""
},
"employer_c": {
"name": "employer_c",
"value": ""
},
"employer_website_c": {
"name": "employer_website_c",
"value": ""
},
"fico_score_c": {
"name": "fico_score_c",
"value": ""
},
"household_income_c": {
"name": "household_income_c",
"value": ""
},
"investment_capital_c": {
"name": "investment_capital_c",
"value": ""
},
"job_title_c": {
"name": "job_title_c",
"value": ""
},
"mbrs_company_c": {
"name": "mbrs_company_c",
"value": ""
},
"membr_type_c": {
"name": "membr_type_c",
"value": ""
},
"nickname_c": {
"name": "nickname_c",
"value": ""
},
"num_properties_financed_c": {
"name": "num_properties_financed_c",
"value": ""
},
"num_properties_owned_c": {
"name": "num_properties_owned_c",
"value": ""
},
"own_primary_residence_c": {
"name": "own_primary_residence_c",
"value": ""
},
"parent_id": {
"name": "parent_id",
"value": ""
},
"parent_name": {
"name": "parent_name",
"value": ""
},
"parent_type": {
"name": "parent_type",
"value": ""
},
"planned_retirement_date_c": {
"name": "planned_retirement_date_c",
"value": false
},
"realestateadvisor_c": {
"name": "realestateadvisor_c",
"value": ""
},
"referral_c": {
"name": "referral_c",
"value": ""
},
"sdira_c": {
"name": "sdira_c",
"value": ""
},
"self_directed_ira_c": {
"name": "self_directed_ira_c",
"value": ""
},
"self_directed_ira_capital_c": {
"name": "self_directed_ira_capital_c",
"value": ""
},
"source_c": {
"name": "source_c",
"value": ""
},
"source_sub_c": {
"name": "source_sub_c",
"value": ""
},
"spouse_c": {
"name": "spouse_c",
"value": ""
},
"user_id1_c": {
"name": "user_id1_c",
"value": ""
},
"user_id_c": {
"name": "user_id_c",
"value": ""
},
"usigndate_c": {
"name": "usigndate_c",
"value": ""
},
"usign_c": {
"name": "usign_c",
"value": ""
},
"longitude_c": {
"name": "longitude_c",
"value": ""
},
"latitude_c": {
"name": "latitude_c",
"value": ""
},
"newmemberform_c": {
"name": "newmemberform_c",
"value": ""
},
"nr_contact_lead_source_c": {
"name": "nr_contact_lead_source_c",
"value": ""
},
"nr_contact_lead_score_c": {
"name": "nr_contact_lead_score_c",
"value": ""
},
"nr_contact_recent_activity_c": {
"name": "nr_contact_recent_activity_c",
"value": ""
}
}
答案 0 :(得分:4)
如果你想使用下划线,那么reduce
就可以解决问题:
var o = _(ugly).reduce(function(m, h) {
m[h.name] = h.value;
return m;
}, { });
答案 1 :(得分:3)
我只是采用直接的方法:
var newArr = {};
for(var key in YOUR_JSON_ARRAY)
newArr[key] = YOUR_JSON_ARRAY[key].value;
答案 2 :(得分:0)
请记住,vardef数组中的约定是将数组索引作为字段名称,但它不是必需的。我想不出一个他们不一样的例子,但你可以创建一个像'my_new_field' => array('name'=>'new_field_c')
这样的数组的字段,它会起作用。