0

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

5
  • Why are you extracting the different values from different objects in the array? The input is supposed to be something like [{"fly": true, "blond": 42, "tragic": false, "top": "Foo"}] where all 4 values are keys of the same object in the array. The fact that you don't use i for anything inside the loop should be an instant mental alert, telling you something is wrong with the code. Commented May 9, 2018 at 2:35
  • one of my TA's told me to add the for loop. I realized it did nothing, but without the for loop I still got the question wrong Commented May 9, 2018 at 2:39
  • 1
    Of course you need a loop. You're supposed to read an array of unknown size, and return an ArrayList of all the parsed objects. Commented May 9, 2018 at 2:47
  • 1
    Wow that was so simple. Stupidest mistake. All I had to do was change 0 to i, then delete all the other JsonObjects I created (besides the first one) in my array list. Commented May 9, 2018 at 2:54
  • Requesting all to not provide full answer to any assignment work, rather give hints. Commented May 9, 2018 at 8:57

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

-1

You are retrieving the first object each time.

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

Change the 0 to i there.

1 Comment

That will not fix the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.