从JsonArray中检索公共和唯一元素

时间:2013-08-19 08:53:58

标签: javascript

我有这种格式的JSON

[
    {"ip":"190","params":{"key":"Thread_State","value":"start"}},
    {"ip":"190","params":{"key":"Rate_Classification","value":"30"}},
    {"ip":"190","params":{"key":"Rate_Evaluation","value":"30"}},
    {"ip":"191","params":{"key":"Thread_State","value":"start"}}, 
    {"ip":"191","params":{"key":"Thread_State","value":"stop"}}
]

我希望根据键和值从此JSON中检索常用和唯一元素。

例如:

常见元素是{“ip”:190 {"key":Thread_State,"value":start}},{“ip”:191 {"key":Thread_State,"value":start}}

我们如何使用JavaScript获得此结果?

1 个答案:

答案 0 :(得分:0)

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
var json_string ='[{"ip":"190","params":{"key":"Thread_State","value":"start"}},{"ip":"190","params":{"key":"Rate_Classification","value":"30"}},{"ip":"190","params":{"key":"Rate_Evaluation","value":"30"}},{"ip":"191","params":{"key":"Thread_State","value":"start"}},{"ip":"191","params":{"key":"Thread_State","value":"stop"}}]';
var dataArray = [];
var encoded = jQuery.parseJSON(json_string);
$.each(encoded, function (index, value) {
    dataArray.push([value["ip"].toString(), value["params"]["key"], value["params"]["value"] ]);
});
alert(dataArray);
</script>