Json回应问题

时间:2015-08-20 08:11:53

标签: json

我有这个回复

  

{“id”:“A148FD5E57B14254B30012BDFA8DB6BE”,“key”:8,“address_1”:   “ILFORD ILFORD CASTLEVIEW GARDENS”,“address_2”:“GREAL LONDON”,   “prop_type”:“T”,“lease_type”:“F”,“sale_price”:“270455”,“lat”:   “51.5756245”,“lng”:“0.0589847”,“paon”:“”,“saon”:“36”,   “sale_history”:[{“key”:8,“sale_date”:“270455”,“sale_price”:   “2005-05-27”}]},{“id”:“8FF1C0D6DC434FC4849BCD09AAFE9DE1”,“key”:   20,“address_1”:“ILFORD ILFORD CASTLEVIEW GARDENS”,“address_2”:   “GREATER LONDON”,“prop_type”:“T”,“lease_type”:“F”,“sale_price”:   “135000”,“lat”:“51.5756245”,“lng”:“0.0589847”,“paon”:“”,“saon”:   “36”,“sale_history”:[{“key”:20,“sale_date”:“135000”,   “sale_price”:“1999-05-27”}]}

但我想像这样转换它

"my_property": {
    "id": "1A936E79FB2042D087D56F98346A24D1",
    "address_1": "CASTLEVIEW GARDENS, ILFORD",
    "address_2": "ILFORD, IG1 3QB",
    "current_estimated": "$232323",
    "price_change_amount": "$23",
    "price_change_percentage": "+12%",
    "avg_pcm_growth_amount": "$342",
    "avg_pcm_growth_percentage": "23%",
    "prop_type": "Flat",
    "lease_type": "Lease",
    "new_not": "N",
    "sale_price": "$275000",
    "sale_date": "2014-08-22",
    "lat": "51.5755682",
    "lng": "0.0534691",
    "sales_history": [
      {
        "sale_date": "2 Dec, 2011",
        "sale_price": "$2123123",
        "price_change_percentage": "+22.2%"
      },
      {
        "sale_date": "2 Dec, 2013",
        "sale_price": "$23432423",
        "price_change_percentage": "+1.2%"
      }
    ]
  },

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:-1)

@vonfry指出:"my_property": { "id":无效json。

你必须将my_property放在另一个对象中,例如。在javascript中:  var my_obj = { "my_property": { "id": ... } }

也许我错了,但它看起来好像你想根据收到的json生成一个新对象。所以也许你想要:

var received = { "id" : ... } // Your received json
var my_property = {};
my_property.id = received.id;
my_property.address_1 = received.address_1;
//And so on for the properties you want to copy from the received object
my_property.current_estimated = "$232323";
//And so on for the extra fields you want to assosciate with my_property

顺便提一句,我猜"sale_history": [ { "key": 8, "sale_date": "270455", "sale_price": "2005-05-27" } ]应该是: "sale_history": [ { "key": 8, "sale_price": "270455", "sale_date": "2005-05-27" } ]