我正试图通过json

时间:2017-12-13 16:50:48

标签: javascript json

我不确定如何在一个问题中总结我想要做的事情。希望我可以解释它,这是有道理的。

我有一个JSON数组,它列出了一堆html滑块元素。它们分为3个控制窗格(基本,接口和高级),每个窗格都有几个滑块和开关。在其他变量中,每个变量都具有“位置”编号和“标签”。

$json ='[{ "panename": "Basic","pane": "[{\"type\":\"slider\",\"label\":\"Sit Time\",\"id\":\"sitTime\",\"position\":\"0\",\"active\":\"true\"},{\"type\":\"slider\",\"label\":\"Snooze Number\",\"id\":\"snoozeNumber\",\"position\":\"3\",\"active\":\"true\"},{\"type\":\"slider\",\"label\":\"Snooze Duration\",\"id\":\"snoozeDuration\",\"position\":\"4\",\"active\":\"true\"},{\"type\":\"slider\",\"label\":\"Threshold\",\"id\":\"threshold\",\"position\":\"7\",\"active\":\"true\"}]"      },      {        "panename": "Interface",        "pane": "[{\"type\":\"slider\",\"label\":\"Volume\",\"position\":\"5\",\"active\":\"true\"},{\"type\":\"slider\",\"label\":\"LED\",\"id\":\"ledBrightness\",\"position\":\"9\",\"active\":\"true\"},{\"type\":\"switch\",\"label\":\"Notifications\",\"id\":\"notifications\",\"position\":\"12\",\"active\":\"true\"},{\"type\":\"switch\",\"label\":\"Notification Sound\",\"id\":\"notificationSound\",\"position\":\"13\",\"active\":\"true\"},{\"type\":\"switch\",\"label\":\"App Alarms\",\"id\":\"appAlarms\",\"position\":\"14\",\"active\":\"true\"}]"      },      {        "panename": "Advanced",        "pane": "[{\"type\":\"slider\",\"label\":\"Flextime\",\"id\":\"flextime\",\"position\":\"11\",\"active\":\"true\"},{\"type\":\"switch\",\"label\":\"Natural Mode\",\"id\":\"naturalMode\",\"position\":\"2\",\"active\":\"true\"},{\"type\":\"switch\",\"label\":\"SCM\",\"id\":\"scm\",\"position\":\"10\",\"active\":\"true\"},{\"type\":\"switch\",\"label\":\"Confirm Save\",\"id\":\"confirmSave\",\"position\":\"8\",\"active\":\"true\"},{\"type\":\"switch\",\"label\":\"Lockout\",\"id\":\"lockout\",\"position\":\"6\",\"active\":\"true\"}]"}]';

我要做的是解析整个事情,最后按位置顺序排列一串标签。请记住,总共有14个滑块/开关,例如,滑块0和1(sitTime,releaseTime)位于“基本”窗格上,开关2(naturalMode)位于“高级”窗格中。我想做这样的事情:

var labelStr='';
for (x=0;x < number of objects; x++){
    if ('position' == x){
      labelStr= labelStr + ',' +  label of object at position x;
    }

//For example:
// if ('position' == 0){
//    labelStr= labelStr +',' + 'sitTime';
//   }

}

最终,我会得到一个字符串:

labelStr= "sitTime, releaseTime, naturalMode, snoozeNumber, snoozeDuration, volume, lockout ..."  

我根本不知道如何解析我的JSON以获得我想要的东西。

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
let json ='[{ "panename": "Basic","pane": "[{\\"type\\":\\"slider\\",\\"label\\":\\"Sit Time\\",\\"id\\":\\"sitTime\\",\\"position\\":\\"0\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"Snooze Number\\",\\"id\\":\\"snoozeNumber\\",\\"position\\":\\"3\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"Snooze Duration\\",\\"id\\":\\"snoozeDuration\\",\\"position\\":\\"4\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"Threshold\\",\\"id\\":\\"threshold\\",\\"position\\":\\"7\\",\\"active\\":\\"true\\"}]"      },      {        "panename": "Interface",        "pane": "[{\\"type\\":\\"slider\\",\\"label\\":\\"Volume\\",\\"position\\":\\"5\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"LED\\",\\"id\\":\\"ledBrightness\\",\\"position\\":\\"9\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"Notifications\\",\\"id\\":\\"notifications\\",\\"position\\":\\"12\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"Notification Sound\\",\\"id\\":\\"notificationSound\\",\\"position\\":\\"13\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"App Alarms\\",\\"id\\":\\"appAlarms\\",\\"position\\":\\"14\\",\\"active\\":\\"true\\"}]"      },      {        "panename": "Advanced",        "pane": "[{\\"type\\":\\"slider\\",\\"label\\":\\"Flextime\\",\\"id\\":\\"flextime\\",\\"position\\":\\"11\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"Natural Mode\\",\\"id\\":\\"naturalMode\\",\\"position\\":\\"2\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"SCM\\",\\"id\\":\\"scm\\",\\"position\\":\\"10\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"Confirm Save\\",\\"id\\":\\"confirmSave\\",\\"position\\":\\"8\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"Lockout\\",\\"id\\":\\"lockout\\",\\"position\\":\\"6\\",\\"active\\":\\"true\\"}]"}]';
json=JSON.parse(json);
let arr=json.map((x)=>(JSON.parse(x.pane)));
arr=[].concat(...arr);
arr.sort((x,y)=>Number(x.position)-Number(y.position));
console.log(arr);
&#13;
&#13;
&#13;