2016-09-19 79 views
2

我擁有內部具有多個對象的JSONObject數據。 我想要做的是,使該json簡單的層次結構。Java將字符串JSON多維對象元素解析爲字符串

JSON數據

{ 
    "Response": { 
     "type": "string", 
     "content": "0000" 
    }, 
    "Data": { 
     "item": [ 
      { 
       "firstname": { 
        "type": "string", 
        "content": "Bryan" 
       }, 
       "lastname": { 
        "type": "string", 
        "content": "Adams" 
       }, 
       "kids": { 
        "item": [ 
         { 
          "name": { 
           "type": "string", 
           "content": "Tommy" 
          }, 
          "age": { 
           "type": "string", 
           "content": "9" 
          } 
         }, 
         { 
          "name": { 
           "type": "string", 
           "content": "Jane" 
          }, 
          "age": { 
           "type": "string", 
           "content": "4" 
          } 
         } 
        ] 
       } 
      }, 
      { 
       "firstname": { 
        "type": "string", 
        "content": "Joey" 
       }, 
       "lastname": { 
        "type": "string", 
        "content": "Cena" 
       }, 
       "kids": { 
        "item": [ 
         { 
          "name": { 
           "type": "string", 
           "content": "Maria" 
          }, 
          "age": { 
           "type": "string", 
           "content": "7" 
          } 
         }, 
         { 
          "name": { 
           "type": "string", 
           "content": "Dany" 
          }, 
          "age": { 
           "type": "string", 
           "content": "3" 
          } 
         } 
        ] 
       } 
      } 
     ] 
    } 
} 

我的代碼

package junk; 

import java.util.Iterator; 
import org.json.JSONException; 
import org.json.JSONObject; 

/** 
* 
* @author Agung 
*/ 
class Foo { 

    private static JSONObject objResponse = new JSONObject(); 

    public static void main(String args[]) throws JSONException { 
     JSONObject jsonObj = new JSONObject("{\"Response\":{\"type\":\"string\",\"content\":\"0000\"},\"Data\":{\"item\":[{\"firstname\":{\"type\":\"string\",\"content\":\"Bryan\"},\"lastname\":{\"type\":\"string\",\"content\":\"Adams\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Tommy\"},\"age\":{\"type\":\"string\",\"content\":\"9\"}},{\"name\":{\"type\":\"string\",\"content\":\"Jane\"},\"age\":{\"type\":\"string\",\"content\":\"4\"}}]}},{\"firstname\":{\"type\":\"string\",\"content\":\"Joey\"},\"lastname\":{\"type\":\"string\",\"content\":\"Cena\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Maria\"},\"age\":{\"type\":\"string\",\"content\":\"7\"}},{\"name\":{\"type\":\"string\",\"content\":\"Dany\"},\"age\":{\"type\":\"string\",\"content\":\"3\"}}]}}]}}"); 
     Foo.getResponseContent(jsonObj); 
     System.out.println(objResponse); 
    } 

    private static void getResponseContent(JSONObject jsonObj) throws JSONException { 
     Iterator<?> keys = jsonObj.keys(); 
     while (keys.hasNext()) { 
      String key = (String) keys.next(); 
      if (jsonObj.get(key) instanceof JSONObject) { 
       JSONObject object = jsonObj.getJSONObject(key); 
       if (object.has("content")) { 
        String content = (String) object.get("content"); 
        objResponse.put(key, content); 
       } else { 
        // if we get here, so the element have multiple node 
        objResponse.put(key, object); 
        getResponseContent(object); 
       } 
      } 
     } 
    } 
} 

我的代碼,我得到這樣的結果:

{ 
    "Response": "0000", 
    "Data": { 
     "item": [ 
      { 
       "firstname": { 
        "type": "string", 
        "content": "Bryan" 
       }, 
       "lastname": { 
        "type": "string", 
        "content": "Adams" 
       }, 
       "kids": { 
        "item": [ 
         { 
          "name": { 
           "type": "string", 
           "content": "Tommy" 
          }, 
          "age": { 
           "type": "int", 
           "content": "9" 
          } 
         }, 
         { 
          "name": { 
           "type": "string", 
           "content": "Jane" 
          }, 
          "age": { 
           "type": "int", 
           "content": "4" 
          } 
         } 
        ] 
       } 
      }, 
      { 
       "firstname": { 
        "type": "string", 
        "content": "Joey" 
       }, 
       "lastname": { 
        "type": "string", 
        "content": "Cena" 
       }, 
       "kids": { 
        "item": [ 
         { 
          "name": { 
           "type": "string", 
           "content": "Maria" 
          }, 
          "age": { 
           "type": "int", 
           "content": "7" 
          } 
         }, 
         { 
          "name": { 
           "type": "string", 
           "content": "Dany" 
          }, 
          "age": { 
           "type": "int", 
           "content": "3" 
          } 
         } 
        ] 
       } 
      } 
     ] 
    } 
} 

僅現場沒有多個元素的工作。 但我想要的結果是:

{ 
    "Response": "0000", 
    "Data": { 
     "item": [ 
      { 
       "firstname": "Bryan", 
       "lastname": "Adams", 
       "kids": { 
        "item": [ 
         { 
          "name": "Tommy", 
          "age": 9 
         }, 
         { 
          "name": "Jane", 
          "age": 4 
         } 
        ] 
       } 
      }, 
      { 
       "firstname": "Joey", 
       "lastname": "Cena", 
       "kids": { 
        "item": [ 
         { 
          "name": "Maria", 
          "age": 7 
         }, 
         { 
          "name": "Dany", 
          "age": 3 
         } 
        ] 
       } 
      } 
     ] 
    } 
} 

我不知道如何從數據字段的對象。

+0

這將是更好的和更清潔的,如果你能在這,而不是變相JSON到Java對象和運行操作如果其他循環。 –

+0

我還沒有在Java中使用JSON,但是在JavaScript中它們提供了將對象轉換爲JSON並返回的字符串化和解析方法。我相信Java有類似的東西。 –

+0

你還可以告訴我objResponse在哪裏聲明?我有一種感覺,物體可能是罪魁禍首。 –

回答

0

下面是創建你想要的格式輸出的程序:像你這樣的,我用遞歸,我所做的改變是:

  • 我處理陣列
  • 我創建了一個新對象收集沒有「內容」鍵的孩子的數據。

下面是代碼:

package test; 

import java.util.Iterator; 

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

public class TestMain { 

    public static void main(String args[]) throws Exception { 
     JSONObject objResponse = new JSONObject(); 
     JSONObject jsonObj = new JSONObject(
       "{\"Response\":{\"type\":\"string\",\"content\":\"0000\"},\"Data\":{\"item\":[{\"firstname\":{\"type\":\"string\",\"content\":\"Bryan\"},\"lastname\":{\"type\":\"string\",\"content\":\"Adams\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Tommy\"},\"age\":{\"type\":\"string\",\"content\":\"9\"}},{\"name\":{\"type\":\"string\",\"content\":\"Jane\"},\"age\":{\"type\":\"string\",\"content\":\"4\"}}]}},{\"firstname\":{\"type\":\"string\",\"content\":\"Joey\"},\"lastname\":{\"type\":\"string\",\"content\":\"Cena\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Maria\"},\"age\":{\"type\":\"string\",\"content\":\"7\"}},{\"name\":{\"type\":\"string\",\"content\":\"Dany\"},\"age\":{\"type\":\"string\",\"content\":\"3\"}}]}}]}}"); 
     getResponseContent(jsonObj, objResponse); 
     System.out.println(objResponse.toString(2)); 
    } 

    private static void getResponseContent(JSONObject jsonObj, JSONObject objResponse) throws JSONException { 

     Iterator<?> keys = jsonObj.keys(); 
     while (keys.hasNext()) { 
      String key = (String) keys.next(); 
      JSONObject child = jsonObj.optJSONObject(key); 
      if (child != null) { 
       if (child.has("content")) { 
        objResponse.put(key, child.get("content")); 
       } else { 
        JSONObject responseChild = new JSONObject(); 
        objResponse.put(key, responseChild); 
        getResponseContent(child, responseChild); 
       } 
      } else { 
       JSONArray children = jsonObj.optJSONArray(key); 
       if (children != null) { 
        JSONArray responseChildren = new JSONArray(); 
        objResponse.put(key, responseChildren); 
        for (int i = 0; i < children.length(); i++) { 
         child = children.getJSONObject(i); 
         JSONObject responseChild = new JSONObject(); 
         responseChildren.put(responseChild); 
         getResponseContent(child, responseChild); 
        } 
       } 
      } 
     } 

    } 
} 

下面是它的輸出:

{ 
    "Response": "0000", 
    "Data": {"item": [ 
    { 
     "firstname": "Bryan", 
     "lastname": "Adams", 
     "kids": {"item": [ 
     { 
      "name": "Tommy", 
      "age": "9" 
     }, 
     { 
      "name": "Jane", 
      "age": "4" 
     } 
     ]} 
    }, 
    { 
     "firstname": "Joey", 
     "lastname": "Cena", 
     "kids": {"item": [ 
     { 
      "name": "Maria", 
      "age": "7" 
     }, 
     { 
      "name": "Dany", 
      "age": "3" 
     } 
     ]} 
    } 
    ]} 
}