简体   繁体   English

JSON解析。 字符串作为对象的Json数组

[英]JSON parsing. String as Json array of objects

The given complete question is, just to clear up any issues: 给定的完整问题是,仅解决所有问题:

   /**
 * q1: Write a public class named ColonialFive with private instance variables named "fly" of 
 * type boolean, "blond" of type int, "tragic" of type boolean, and "top" of type String.
 * 
 * Then write a public method inside the Problem Set class named "parseColonialFive" that 
 * takes a String as a parameter and returns an ArrayList of ColonialFive. This method will 
 * parse the input String as a properly formatted Json array of objects where each object 
 * represents the values for the instance of ColonialFive where the keys of each Json object 
 * are the instance variable names and the values are the values to which they should be set. 
 * Return an ArrayList of instances of ColonialFive with the instance variables matching the 
 * values from the Json objects. The order of instances in the ArrayList must match the order 
 * of objects in the Json array
 */

The code I have is: 我的代码是:

    public class ColonialFive {
    private boolean fly;
    private int blond;
    private boolean tragic;
    private String top;

    public ColonialFive(boolean a, int b, boolean c, String d) {
        this.fly = a;
        this.blond = b;
        this.tragic = c;
        this.top = d;
    }
    public void setFly(boolean a) {
        this.fly = a;
    }
    public void setBlond(int b) {
        this.blond = b;
    }
    public void setTragic(boolean c) {
        this.tragic = c;
    }
    public void setTop(String d) {
        this.top = d;
    }

}

public ArrayList<ColonialFive> parseColonialFive(String a1) {
ArrayList<ColonialFive> data = new ArrayList<ColonialFive>();
JsonValue jsonValue = Json.parse(a1);
JsonArray jsonArray = jsonValue.asArray();

for(int i =0;i<jsonArray.size();i++) {              

    JsonObject thisAble = jsonArray.get(0).asObject();
        boolean a = thisAble.get("fly").asBoolean();

        JsonObject jsonObject2 = jsonArray.get(1).asObject();
        int b = jsonObject2.get("blond").asInt();

        JsonObject civ = jsonArray.get(2).asObject();
        boolean c = civ.get("tragic").asBoolean();

        JsonObject pri = jsonArray.get(3).asObject();
        String d =pri.get("top").asString();

        ColonialFive h = new ColonialFive(a,b,c,d);
        h.setFly(a);
        h.setBlond(b);
        h.setTragic(c);
        h.setTop(d);
        data.add(h);
}
return data;
}

When I run the code I do not get correct values for each instant variable. 当我运行代码时,没有为每个即时变量获取正确的值。 I am not sure what could be going wrong, I have tried to write this code several different ways as well. 我不确定会出什么问题,我也尝试用几种不同的方式编写此代码。

I am not sure if I should be using JsonValue or not, it should work as is and others have even told me so 我不确定是否应该使用JsonValue,它是否应该按原样工作,其他人甚至告诉过我,

Just Try this... 试试这个...

public ArrayList<ColonialFive> parseColonialFive(String a1) {
    ArrayList<ColonialFive> data = new ArrayList<ColonialFive>();       
    JSONArray jArray = new JSONArray(a1); 
    if (jArray != null) { 
       for (int i=0;i<jArray.length();i++){ 
        Gson gson = new Gson();                     
        ColonialFive col= gson.fromJson(jArray.get(i).toString(), ColonialFive.class);
        data.add(col);
       } 
    } 

    return data;
}


public static void main(String[] args) {
    String a1= "[{\"fly\": true, \"blond\": 42, \"tragic\": false, \"top\": \"Foo\"},{\"fly\": true, \"blond\": 42, \"tragic\": false, \"top\": \"Foo\"}]";
    ArrayList<ColonialFive> data = new ColonialFive().parseColonialFive(a1);

    System.out.println(data);   

}

for this I am using two jar files gson-2.2.2.jar and json.jar 为此,我正在使用两个jar文件gson-2.2.2.jarjson.jar

You can try this : 您可以尝试以下方法:

public ArrayList<ColonialFive> parseColonialFive(String a1) {
    ArrayList<ColonialFive> data = new ArrayList<ColonialFive>();       
    JSONArray jArray = new JSONArray(a1); 
    if (jArray != null) { 
       for (int i=0;i<jArray.length();i++){ 
        Gson gson = new Gson();                     
        ColonialFive col= gson.fromJson(jArray.get(i).toString(), ColonialFive.class);
        data.add(col);
       } 
    } 

    return data;
}

You are retrieving the first object each time. 您每次都在检索第一个对象。

JsonObject thisAble = jsonArray.get(0).asObject();

Change the 0 to i there. 0更改为i

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

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