3

我在 PHP 页面中有这个 JSON 字符串:

{
  "elements": [{
    "type": "pie",
    "alpha": 0.3,
    "animate": [{
      "type": "fade"
    }, {
      "type": "bounce",
      "distance": 5
    }],
    "start-angle": 0,
    "tip": "#val# de #total# #percent#",
    "colours": ["#d01f3c", "#356aa0", "#C79810"],
    "values": [{
      "value": 1,
      "label": "procesador amd sempron 140"
    }, {
      "value": 1,
      "label": "procesador sempron le130"
    }, {
      "value": 1,
      "label": "procesador amd a4-3300 x2"
    }, {
      "value": 1,
      "label": "procesador intel celeron g530"
    }]
  }],
  "title": {
    "text": "Procesadores, Reinicio",
    "style": "color: #356aa0; font-size: 20px"
  },
  "bg_colour": "#FFFFFF",
  "x_axis": null
}

我这样称呼它:

$.getJSON("restart_proce.php", function(json)
{    
console.log(json);

我需要将其转换为:

[{\"value\": 1, \"label\": \"procesador amd sempron 140\" }, { \"value\": 1, \"label\": \"procesador sempron le130\" }, { \"value\": 1, \"label\": \"procesador amd a4-3300 x2\" }, { \"value\": 1, \"label\": \"procesador intel celeron g530\" } ]

我正在尝试删除这样的元素:

delete json.elements[3];

但它不会删除任何东西。我怎样才能让它工作?

4

4 回答 4

2

试试这个:

json.elements.splice(3, 1);

请参阅:Array.splice

于 2012-12-26T18:05:31.860 回答
2

从数组中删除一个项目:

有几种方法。拼接方法是最通用的:

data.items.splice(3, 1); // Removes three items starting with the 2nd,

splice 修改原始数组,并返回您删除的项目的数组。

于 2012-12-26T18:07:36.210 回答
1

直接修改之前的值console.log(json)

json= json.elements[0].values

或者在restart_proce.phpphp页面中只是回显

echo json_encode($data['elements'][0]['values']); // if associative array is used.
于 2012-12-26T18:11:11.413 回答
0

试试这个:

var data = {"result":[
  {"FirstName":"Test1","LastName":"User","Email":"test@test.com","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"},
  {"FirstName":"user","LastName":"user","Email":"u@u.com","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"},
  {"FirstName":"Ropbert","LastName":"Jones","Email":"Robert@gmail.com","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"},
  {"FirstName":"hitesh","LastName":"prajapti","Email":"hitesh.prajapati@phptalent.com","City":"","State":"","Country":"","Status":"True","iUserID":"78"}
  ]
}
alert(data.result);
delete data.result[3];
alert(data.result);

工作JSFiddle

或者您可以使用splice从数组中删除元素。

于 2012-12-26T18:12:39.800 回答