how to iterate through json objects in java

前端 未结 3 1901
离开以前
离开以前 2021-01-07 00:35

I am trying to iterate through my json file and get required details here is my json

{
\"000\": {
    \"component\": \"c\",
    \"determinantType\": \"dt\",         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-07 00:56

    There's not a JSONArray, only a few JSONObjects. Iterate the keys of the main JSONObject with JSONObject.keys().

    public static final String COMPONENT = "component";
    public static final String DT = "determinantType";
    public static final String D = "determinant": "d";
    public static final String HEADER = "header";
    public static final String DV = "determinantvalue";
    
    JSONObject jso = getItFromSomewhere();
    for (Object key : jso.keys()) {
        JSONObject subset = jso.getJSONObject(key);
        String d = subset.getString(D);
        String header = subset.getString(HEADER);
        String dv = subset.getString(DV);
        System.out.println(key + " " + header + " " + d + " " + dv);
    }
    

提交回复
热议问题