Basically I have:
基本上我有:
JSONArray j = new JSONArray();
j.add(new JSONObject()); //JSONObject has a bunch of data in it
j.add(new JSONArray()); //JSONArray has a bunch of data in it
And now I would like to turn that JSONArray
into a JSONObject
with the same information in it. So that I can pass around the Object and then when I want I can grab all the information out of the object. Any help would be appreciated, Thanks.
现在我想将JSONArray转换为具有相同信息的JSONObject。这样我就可以传递Object然后当我想要时我可以从对象中获取所有信息。任何帮助将不胜感激,谢谢。
4 个解决方案
#1
17
Typically, a Json object would contain your values (including arrays) as named fields within. So, something like:
通常,Json对象将包含您的值(包括数组)作为其中的命名字段。所以,像:
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName",ja);
Which in JSON will be {"arrayName":[...]}.
在JSON中将是{“arrayName”:[...]}。
#2
2
Code:
码:
List<String> list = new ArrayList<String>();
list.add("a");
JSONArray array = new JSONArray();
for (int i = 0; i < list.size(); i++) {
array.put(list.get(i));
}
JSONObject obj = new JSONObject();
try {
obj.put("result", array);
} catch (JSONException e) {
e.printStackTrace();
}
#3
1
Can't you originally get the data as a JSONObject?
您最初是否可以将数据作为JSONObject获取?
Perhaps parse the string as both a JSONObject and a JSONArray in the first place? Where is the JSON string coming from?
也许首先将字符串解析为JSONObject和JSONArray? JSON字符串来自哪里?
I'm not sure that it is possible to convert a JsonArray into a JsonObject.
我不确定是否可以将JsonArray转换为JsonObject。
I presume you are using the following from json.org
我认为您正在使用json.org中的以下内容
-
JSONObject.java
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.JSONObject.java JSONObject是名称/值对的无序集合。它的外部形式是一个用大括号括起来的字符串,名称和值之间有冒号,值和名称之间有逗号。内部表单是一个对象,具有用于按名称访问值的get()和opt()方法,以及用于按名称添加或替换值的put()方法。值可以是以下任何类型:Boolean,JSONArray,JSONObject,Number和String,或JSONObject.NULL对象。
-
JSONArray.java
A JSONArray is an ordered sequence of values. Its external form is a string wrapped in square brackets with commas between the values. The internal form is an object having get() and opt() methods for accessing the values by index, and put() methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.JSONArray.java JSONArray是一个有序的值序列。它的外部形式是一个用方括号括起来的字符串,在值之间用逗号表示。内部表单是一个对象,具有通过索引访问值的get()和opt()方法,以及用于添加或替换值的put()方法。值可以是以下任何类型:Boolean,JSONArray,JSONObject,Number和String,或JSONObject.NULL对象。
#4
1
I have JSONObject like this: {"status":[{"Response":"success"}]}
.
我有这样的JSONObject:{“status”:[{“Response”:“success”}]}。
If I want to convert the JSONObject value, which is a JSONArray into JSONObject automatically without using any static value, here is the code for that.
如果我想在不使用任何静态值的情况下自动将JSONArray值(JSONArray)转换为JSONObject,则可以使用以下代码。
JSONArray array=new JSONArray();
JSONObject obj2=new JSONObject();
obj2.put("Response", "success");
array.put(obj2);
JSONObject obj=new JSONObject();
obj.put("status",array);
Converting the JSONArray to JSON Object:
将JSONArray转换为JSON对象:
Iterator<String> it=obj.keys();
while(it.hasNext()){
String keys=it.next();
JSONObject innerJson=new JSONObject(obj.toString());
JSONArray innerArray=innerJson.getJSONArray(keys);
for(int i=0;i<innerArray.length();i++){
JSONObject innInnerObj=innerArray.getJSONObject(i);
Iterator<String> InnerIterator=innInnerObj.keys();
while(InnerIterator.hasNext()){
System.out.println("InnInnerObject value is :"+innInnerObj.get(InnerIterator.next()));
}
}
#1
17
Typically, a Json object would contain your values (including arrays) as named fields within. So, something like:
通常,Json对象将包含您的值(包括数组)作为其中的命名字段。所以,像:
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName",ja);
Which in JSON will be {"arrayName":[...]}.
在JSON中将是{“arrayName”:[...]}。
#2
2
Code:
码:
List<String> list = new ArrayList<String>();
list.add("a");
JSONArray array = new JSONArray();
for (int i = 0; i < list.size(); i++) {
array.put(list.get(i));
}
JSONObject obj = new JSONObject();
try {
obj.put("result", array);
} catch (JSONException e) {
e.printStackTrace();
}
#3
1
Can't you originally get the data as a JSONObject?
您最初是否可以将数据作为JSONObject获取?
Perhaps parse the string as both a JSONObject and a JSONArray in the first place? Where is the JSON string coming from?
也许首先将字符串解析为JSONObject和JSONArray? JSON字符串来自哪里?
I'm not sure that it is possible to convert a JsonArray into a JsonObject.
我不确定是否可以将JsonArray转换为JsonObject。
I presume you are using the following from json.org
我认为您正在使用json.org中的以下内容
-
JSONObject.java
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.JSONObject.java JSONObject是名称/值对的无序集合。它的外部形式是一个用大括号括起来的字符串,名称和值之间有冒号,值和名称之间有逗号。内部表单是一个对象,具有用于按名称访问值的get()和opt()方法,以及用于按名称添加或替换值的put()方法。值可以是以下任何类型:Boolean,JSONArray,JSONObject,Number和String,或JSONObject.NULL对象。
-
JSONArray.java
A JSONArray is an ordered sequence of values. Its external form is a string wrapped in square brackets with commas between the values. The internal form is an object having get() and opt() methods for accessing the values by index, and put() methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.JSONArray.java JSONArray是一个有序的值序列。它的外部形式是一个用方括号括起来的字符串,在值之间用逗号表示。内部表单是一个对象,具有通过索引访问值的get()和opt()方法,以及用于添加或替换值的put()方法。值可以是以下任何类型:Boolean,JSONArray,JSONObject,Number和String,或JSONObject.NULL对象。
#4
1
I have JSONObject like this: {"status":[{"Response":"success"}]}
.
我有这样的JSONObject:{“status”:[{“Response”:“success”}]}。
If I want to convert the JSONObject value, which is a JSONArray into JSONObject automatically without using any static value, here is the code for that.
如果我想在不使用任何静态值的情况下自动将JSONArray值(JSONArray)转换为JSONObject,则可以使用以下代码。
JSONArray array=new JSONArray();
JSONObject obj2=new JSONObject();
obj2.put("Response", "success");
array.put(obj2);
JSONObject obj=new JSONObject();
obj.put("status",array);
Converting the JSONArray to JSON Object:
将JSONArray转换为JSON对象:
Iterator<String> it=obj.keys();
while(it.hasNext()){
String keys=it.next();
JSONObject innerJson=new JSONObject(obj.toString());
JSONArray innerArray=innerJson.getJSONArray(keys);
for(int i=0;i<innerArray.length();i++){
JSONObject innInnerObj=innerArray.getJSONObject(i);
Iterator<String> InnerIterator=innInnerObj.keys();
while(InnerIterator.hasNext()){
System.out.println("InnInnerObject value is :"+innInnerObj.get(InnerIterator.next()));
}
}