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
}
]
}
}
]
}
}
我不知道如何从数据字段的对象。
这将是更好的和更清洁的,如果你能在这,而不是变相JSON到Java对象和运行操作如果其他循环。 –
我还没有在Java中使用JSON,但是在JavaScript中它们提供了将对象转换为JSON并返回的字符串化和解析方法。我相信Java有类似的东西。 –
你还可以告诉我objResponse在哪里声明?我有一种感觉,物体可能是罪魁祸首。 –