Java 将字符串转换为 JsonArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18213104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:21:54  来源:igfitidea点击:

Convert String to JsonArray

javaandroidjson

提问by DieselPower

how to properly convert this String to a jsonArray?

如何正确地将此字符串转换为 jsonArray?

{
    "myArray": [
    {   "id": 1,
        "att1": 14.2,
        "att2": false },
    {   "id": 2,
        "att1": 13.2,
        "att2": false },
    {   "id": 3,
        "att1": 13,
        "att2": false }
  ]}

An JSONArray jArray = new JSONArray(STRING_FROM_ABOVE);results in jArray.length = 1Its my first time to get in touch with json :)

一个JSONArray jArray = new JSONArray(STRING_FROM_ABOVE);在结果jArray.length = 1它的我第一次使用JSON取得联系:)

采纳答案by Organ

Try this :

尝试这个 :

JSONObject jObject = new JSONObject(STRING_FROM_ABOVE);
JSONArray jArray = jObject.getJSONArray("myArray");

The "string_from_above" is not a Json Array, it's a Json object, containing one attribute (myArray) which is a Json Array ;)

“string_from_above”不是一个 Json 数组,它是一个 Json 对象,包含一个属性 (myArray),它是一个 Json 数组;)

You can then do :

然后你可以这样做:

for (int i = 0; i < jArray.length(); i++) {
        JSONObject jObj = jArray.getJSONObject(i);
        System.out.println(i + " id : " + jObj.getInt("id"));
        System.out.println(i + " att1 : " + jObj.getDouble("att1"));
        System.out.println(i + " att2 : " + jObj.getBoolean("att2"));
}

回答by PizzaTheHut

Have a look at JQuery, and in particular the Parse function. This will parse a Json Array into an Object directly.

看看 JQuery,特别是 Parse 函数。这将直接将 Json Array 解析为 Object。

回答by canova

While parsing, add values to objects and ArrayLists or however the way you want to use it. Good luck.

在解析时,将值添加到对象和 ArrayLists 或您想要的使用方式。祝你好运。

JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list

回答by TwentyMiles

Just to throw another method into the mix here, I'd like to recommend taking a look at Gson. Gson is a library that makes serialization to and deserialization from Java objects a snap. For example, with your string, you could do this:

只是为了在这里混合使用另一种方法,我想推荐看看Gson。Gson 是一个库,它使 Java 对象的序列化和反序列化变得轻而易举。例如,使用您的字符串,您可以执行以下操作:

// Declare these somewhere that is on the classpath
public class ArrayItem{
    public int id;
    public double att1;
    public boolean att2;
}

public class Container{
    public List<ArrayItem> myArray;
}

// In your code
Gson gson = new Gson();

Container container = gson.fromJson(json, Container.class);

for(ArrayItem item : container.myArray){
    System.out.println(item.id);  // 1, 2, 3
    System.out.println(item.att1);  // 14.2, 13.2, 13.0
    System.out.println(item.att2);  // false, false, false
}

Similarly, you can go backwards very easily too.

同样,您也可以非常轻松地倒退。

String jsonString = gson.toJson(container);
// jsonString no contains something like this:
// {"myArray":[{"id":1,"att1":14.2,"att2":false},{"id":2,"att1":13.2,"att2":false},{"id":3,"att1":13.0,"att2":false}]}

The primary benefit that using something like Gson provides is that you can now use all of Java's type checking by default, instead of having to manage attribute names and types yourself. It also allows you to do some fancy stuff like replicating type hierarchies that make managing large numbers of json messages a snap. It works great with Android, and the jar itself is tiny and doesn't require any additional dependencies.

使用 Gson 之类的东西的主要好处是,您现在可以默认使用 Java 的所有类型检查,而不必自己管理属性名称和类型。它还允许您做一些奇特的事情,例如复制类型层次结构,使管理大量 json 消息变得轻而易举。它适用于 Android,而且 jar 本身很小,不需要任何额外的依赖项。