简体   繁体   English

如何将JSONArray转换为JSONObject?

[英]How can I turn a JSONArray into a JSONObject?

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. 现在我想将JSONArray变成一个JSONObject其中包含相同的信息。 So that I can pass around the Object and then when I want I can grab all the information out of the object. 这样我就可以传递Object然后当我想要时我可以从对象中获取所有信息。 Any help would be appreciated, Thanks. 任何帮助将不胜感激,谢谢。

Typically, a Json object would contain your values (including arrays) as named fields within. 通常,Json对象将包含您的值(包括数组)作为其中的命名字段。 So, something like: 所以,像:

JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName",ja);

Which in JSON will be {"arrayName":[...]}. 在JSON中将是{“arrayName”:[...]}。

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();
}

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()));


 }
}

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? 也许首先将字符串解析为JSONObject和JSONArray? Where is the JSON string coming from? 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 JSONObject.java
    A JSONObject is an unordered collection of name/value pairs. JSONObject是名称/值对的无序集合。 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. 内部表单是一个对象,具有用于按名称访问值的get()和opt()方法,以及用于按名称添加或替换值的put()方法。 The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object. 值可以是以下任何类型:Boolean,JSONArray,JSONObject,Number和String,或JSONObject.NULL对象。

  • JSONArray.java JSONArray.java
    A JSONArray is an ordered sequence of values. JSONArray是一个有序的值序列。 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. 内部表单是一个对象,具有通过索引访问值的get()和opt()方法,以及用于添加或替换值的put()方法。 The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object. 值可以是以下任何类型:Boolean,JSONArray,JSONObject,Number和String,或JSONObject.NULL对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM