如何过滤json对象数据?

CSK

输入Json对象:

{"data":[
    {"itemID":100,"Testcase1":"aaa","status":"Active"},
    {"itemID":100,"Testcase1":"bbb","status":"No"},
    {"itemID":100,"Testcase1":"ccc","status":"Active"},
    {"itemID":101,"Testcase1":"zzz","status":"Active"}
]}

Json对象的预期输出:

[{
    "itemID":"100",
    "Testcase1":"aaa", 
    "Testcase1":"bbb", 
    "Testcase1":"ccc", 
    "status":"Active",
    "status":"No",
    "status":"Active"
},
{
    "itemID":"101",
    "Testcase1":"zzz",
    "status":"Active"
}]

有人帮我吗?

阿卡什·萨拉瓦吉(Akash Sarawagi)

可能对您有帮助。这不是正确的方法。因为对象中不允许使用重复键。无论您的要求如何。我已经创建了您所需格式的字符串。

请检查FIDDLE

var dataVal={"data":[
     {"itemID":100,"Testcase1":"aaa","status":"Active"},
     {"itemID":100,"Testcase1":"bbb","status":"No"},
     {"itemID":100,"Testcase1":"ccc","status":"Active"},
     {"itemID":101,"Testcase1":"zzz","status":"Active"}
]};

var arrayVal = dataVal.data;
var DistinctID=[];
var resultStr="";
for (var elements in arrayVal){

     if($.inArray(arrayVal[elements].itemID, DistinctID) == -1){
      DistinctID.push(arrayVal[elements].itemID)
     }
}

for (var items in DistinctID){
    resultStr +='{"itemID":"'+DistinctID[items]+'",';
    for (var elements in arrayVal){
        if(arrayVal[elements].itemID==DistinctID[items]){
            resultStr +='"Testcase1":"'+arrayVal[elements].Testcase1+'",';
            resultStr +='"status":"'+arrayVal[elements].status+'",';
        }
    }

resultStr = resultStr.substring(0, resultStr.length - 1);

    resultStr= resultStr+'},';

}
resultStr = resultStr.substring(0, resultStr.length - 1);
resultStr='['+resultStr+']';
alert((resultStr));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章