JSON解析。 String作为Json对象数组

时间:2018-05-09 02:26:42

标签: java arrays json parsing

给出完整的问题是,只是为了解决任何问题:

   /**
 * 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
 */

我的代码是:

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

当我运行代码时,我没有为每个即时变量获得正确的值。我不确定会出现什么问题,我也尝试过几种不同的方式来编写代码。

我不确定我是否应该使用JsonValue,它应该按原样工作,其他人甚至告诉我这样

3 个答案:

答案 0 :(得分:1)

试试这个......

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

}

为此我使用两个jar文件 gson-2.2.2.jar json.jar

答案 1 :(得分:1)

你可以试试这个:

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

答案 2 :(得分:-1)

您每次都在检索第一个对象。

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

0更改为i