将嵌套的JSON字符串解析为Java中的HashMap列表:

时间:2018-12-31 16:25:58

标签: arrays json

我有一个JSONString,可以根据需要从其获取数据的REST URL进行更改。我需要根据JSONArray和JSONObject将JSON字符串存储到Map列表中。我可以获取JSONObject值,但尚未设置。我使用了如下代码:

    public class JSONFileHandler {
    private static StringBuilder strOut = new StringBuilder();
     private static List<LinkedHashMap<String, Object>> listOfValues = new
      ArrayList<LinkedHashMap<String, Object>>();
    static LinkedHashMap<String, Object> listOfMapValues =new LinkedHashMap <String,Object>();
    public static void main(String[] args) {try {
        String stringResponse =Test.getJSONObject();

        JSONObject inputJson = new JSONObject(stringResponse);
        List<String> lst = new ArrayList<String>();
        lst = findKeysOfJsonObject(inputJson, lst);
            for (int i = 0; i < listOfValues.size(); i++) {
            LinkedHashMap<String,Object> treemap=listOfValues.get(i);
            for (Map.Entry<String, Object> entry : treemap
                    .entrySet()) {
            System.out.println(""+entry.getKey()+":"+ entry.getValue());
            }
        }
        } catch (Exception e) {
        }
    }
   private static List<String> findKeysOfJsonObject(JSONObject jsonIn,
        List<String> keys) throws JSONException {

    Iterator<String> itr = jsonIn.keys();
    List<String> keysFromObj = makeList(itr);
    keys.addAll(keysFromObj);
    int count = 1;
    itr = jsonIn.keys();
        while (itr.hasNext()) {
          String itrStr = itr.next();
           JSONObject jsout = null;JSONArray jsArr = null;
            strOut.append(itrStr + "," + jsonIn.get(itrStr));
            strOut.append(System.lineSeparator());
            listOfMapValues.put(itrStr, jsonIn.get(itrStr));
              if (jsonIn.get(itrStr).getClass() == JSONObject.class) {
                jsout = jsonIn.getJSONObject(itrStr);
                 findKeysOfJsonObject(jsout, keys);
           } else if (jsonIn.get(itrStr).getClass() == JSONArray.class) {
            jsArr = jsonIn.getJSONArray(itrStr);
            keys.addAll(findKeysOfJsonArray(jsArr, keys));          }
        listOfValues.add(listOfMapValues);          }
    return keys;
      }
    private static List<String> findKeysOfJsonArray(JSONArray jsonIn,
      List<String> keys) throws JSONException {List<String> keysFromArr = 
      new ArrayList<>();
    if (jsonIn != null && jsonIn.length() != 0) {
    for (int i = 0; i < jsonIn.length(); i++) {
        JSONObject jsonObjIn = jsonIn.getJSONObject(i);
        keysFromArr = findKeysOfJsonObject(jsonObjIn, keys);
        }
    }
    listOfValues.add(listOfMapValues);
    return keysFromArr;
    }
    public static List<String> makeList(Iterator<String> iter) {   
         List<String> list = new ArrayList<String>();
        while (iter.hasNext()) {
        String val = iter.next();
            list.add(val);
        }
        return list;
        }
}

StringResponse = {“结果”:[{“字段”:{“摘要”:“测试摘要”,“ customfield_10028”:{“值”:“是”},“描述”:“测试描述”,“ project“:{” name“:” jon doe“}}},{” fields“:{” summary“:” Test Summary2“,” customfield_10028“:{” value“:” Yes2“},” description“:”测试说明2“,”项目“:{”名称“:”名称2“}}}]}

**** ResponseList应该类似于:****      { {fields = {“ summary”:“测试摘要”,“ customfield_10028 = {” value“:”是“},” description“ =”测试描述“,” project“ = {” name“:” name“}},摘要=测试摘要,customfield_10028 = {“值”:“是”},值=是,说明=测试说明,项目= {“名称”:“名称”},名称=名称}, {fields = {“ summary”:“ Test Summary2”,“ customfield_10028:{” value“:” Yes2“},” description“:” Test Description2“,” project“:{” name“:” name2“}}, summary =测试Summary2,customfield_10028 = {“ value”:“ Yes2”},value = Yes2,说明=测试说明2,项目= {“ name”:“ name2”},name = name2}     }

0 个答案:

没有答案