从字符串数组创建Json数组

时间:2018-03-20 16:13:33

标签: java arrays json string

我正在尝试实现以下json对象:

其中sid将是所有json对象的标题,如下所示:

export default class OlderSibling extends Component {
  state = {
    currentCity: city //(that was gathered from a bunch of other steps)
  };

  componentDidUpdate(prevProps, prevState) {
    if (prevState.currentCity !== this.state.currentCity) {
      this.props.updateCurrentCity(this.state.currentCity);
    }
  }

  render() {
    return(
    );
  }
}

我试图使用此代码来实现我试图达到的目标:

   {
  "sid": {
    "1662809241632798": {
      "JSON": [
        {
          "path": "root/AuthenticationUnixTime",
          "val1": "1521560221877",
          "val2": "null",
          "comp": "false"
        },
        {
          "path": "root/ClientDate",
          "val1": "1521560221877",
          "val2": "1519818708657",
          "comp": "false"
        },
        {
          "path": "root/ContentSize",
          "val1": "18064",
          "val2": "18029",
          "comp": "false"
        }
      ],
      "XML": [
        {
          "path": "root / recording / procIp",
          "val1": "172.22.6.26",
          "val2": "null",
          "comp": "false"
        },
        {
          "path": "root / recording / procIp",
          "val1": "172.22.6.26",
          "val2": "null",
          "comp": "false"
        },
        {
          "path": "root / recording / messagesSize",
          "val1": "13529",
          "val2": "11982",
          "comp": "false"
        }
      ]
    }
  }
}

其中" json"是字符串数组," XML"是另一个将添加到json的项目。

我的代码产生的是:

public void newMethod(){

       JSONArray jsonArray = new JSONArray();
       json=removeNullFromArrayString(json);
       JSONObject mainObj = new JSONObject();
       mainObj.put("SID",sid);
       try{
           for(int i=0;i<json.length;i++){
               JSONObject list = new JSONObject();
               list.put("JSON",json[i]);

               jsonArray.add(list);
           }

            System.out.println(jsonArray.toString());


       }catch (JSONException e1) {
           // TODO Auto-generated catch block
           e1.printStackTrace();
       }


   }

对此问题的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

其中一个问题是,如果您还要打印sid,则应打印 mainObj (而不是 jsonArray )。还调用toString方法,即:

System.out.println(mainObj.toString());

是多余的。在打印期间自动调用toString方法,不需要显式调用:

System.out.println(mainObj);

答案 1 :(得分:0)

我使用了示例字符串来创建具有您想要的相同格式的JSONObject。希望这有帮助

String jsonString = "[path: root / ClientDate, val1: 1521560221877,val2:1519818708657, comp: false],[path: root / ClientDate, val1: 1521560221877,val2:1519818708657, comp: false]";
    JSONObject mainObj = new JSONObject();
    JSONObject child = new JSONObject();
    JSONObject list;
    JSONObject objectId;

    try {
        String[] json1 = jsonString.split("(?=(,\\[))(,\\[)");
        for (String string : json1) {
            string = string.replaceAll("[\\[\\]]", "");
            String[] json = string.split(",");
            list = new JSONObject();
            for (int i = 0; i < json.length; i++) {
                String[] object = json[i].split(":");
                list.put(object[0], object[1]);
            }

            child.append("JSON", list);

        }
        objectId = new JSONObject();
        objectId.put("123456798", child);
        mainObj.put("SID", objectId);
        System.out.println(mainObj);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
  

印刷输出

     

{“SID”:{“123456798”:{“JSON”:[{“path”:“root / ClientDate”,“val2”:“1519818708657”,“val1”:“1521560221877”,“comp”: “false”},{“path”:“root / ClientDate”,“val2”:“1519818708657”,“val1”:“1521560221877”,“comp”:“false”}]}}}