如何使用iterator java将JSONObject转换为所有键的新Map [英] How to convert JSONObject to new Map for all its keys using iterator java

查看:241
本文介绍了如何使用iterator java将JSONObject转换为所有键的新Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSONObject

I have a JSONObject

{"2016":{"12":{"20":{"19":{"DonationTime":11111111111,"Donation":10}}}}}

使用每个键将其转换为新地图

I want to convert it to new map with each keys

int i = 0;
for (Iterator<String> keysItr = object.keySet().iterator(); keysItr.`hasNext(); i++) {
            String key = keysItr.next();
            Object value = object.get(key);

            if(value instanceof JSONObject) {
                value = toMap((JSONObject) value);

                map.put(key, value);
            }

        }

        SOP(map);   //but here i want to get 4 maps
    }

我想得到4张地图像

hourMap[19] = "{"DonationTime":11111111111,"Donation":10}";
dayMap[20] = "{"19":{"DonationTime":11111111111,"Donation":10}}";
monthMap[12] = "{"12":{"20":{"19":{"DonationTime":11111111111,"Donation":10}}}";
yearMap[2016] = "{"12":{"20":{"19":{"DonationTime":11111111111,"Donation":10}}}";

我正在使用for循环,但无法为i增加值。

I am using for loop yet i am unable to get incremented value for i.

推荐答案

那么你可以简单地将JSON对象转换成一张地图,然后从那里你可以很容易地取出你感兴趣的四张地图

Well u can simply convert the JSON object into a map and then from there u can easily take out the four maps u interested in

这里是一个简单的例子

(注意下面的代码在一个大的JSON图上会导致一些问题,因为它是基于递归的转换)

(watch out the code below on a big JSON graph can cause u some problems since it is a recursion based conversion)

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

public class JsonMapConverter {

public static void main(String... x) throws Exception {

    String jsonString = "{\"2016\":{\"12\":{\"20\":{\"19\":{\"DonationTime\":11111111111,\"Donation\":10}}}}}";
    JSONObject json  = new JSONObject(jsonString);
    Map<String,Object> yearMap = toMap(json);

    String year =  yearMap.keySet().iterator().next();
    Map<String,Object> monthMap = ((Map<String, Object>) yearMap.get(year));

    String month = monthMap.keySet().iterator().next();
    Map<String,Object> dayMap = (Map<String, Object>) monthMap.get(month);

    String day = dayMap.keySet().iterator().next();
    Map<String,Object> hourMap = (Map<String, Object>) dayMap.get(day);

    System.out.println(yearMap);
    System.out.println(monthMap);
    System.out.println(dayMap);
    System.out.println(hourMap);
}

public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap<String, Object>();

    Iterator<String> keysItr = object.keys();
    while(keysItr.hasNext()) {
        String key = keysItr.next();
        Object value = object.get(key);

        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }

        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        map.put(key, value);
    }
    return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
    List<Object> list = new ArrayList<Object>();
    for(int i = 0; i < array.length(); i++) {
        Object value = array.get(i);
        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }

        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        list.add(value);
    }
    return list;
  }
}

用于JSON映射转换我使用此代码答案(将JSON字符串转换为HashMap

for JSON to map conversion i use the code from this answer (Convert a JSON String to a HashMap)

该代码是基于json字符串编写的,您可以根据您的需要调整代码,以防json中出现多年,月和日的情况。

the code was written based on the json string, u may adjust the code according to your needs in case of multiple years,month and days presents in the json

这篇关于如何使用iterator java将JSONObject转换为所有键的新Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆