1

I'm trying to parse a JSON file with GSON with the following structure:

{
  "state": 0,
  "orders": {
    "1": {
      "idOrder": "1564",
      "price": "7.99",
      },
    "3": {
      "idOrder": "4896",
      "price": "9.99",
      },
    "7": {
      "idOrder": "4896",
      "price": "10.99",
      }
  }
}

I made the classes as same structure (with setters and getters)

public class myJson {
private int error;
private Orders orders; }

public class Orders {
@SerializedName("1")
private _1 _1;}

public class _1 {
private String idOrder;
private String price;}

and in main I am using like this:

       Gson gson = new Gson();
       BufferedReader br = new BufferedReader (new FileReader("base.json"));
       Json jsonOffline = gson.fromJson(br, myJson.class);
       System.out.println("1 - Orderid: " + jsonOffline.getOrders().get1().getidOrder() );

*

And it works perfectly printing: "1 - Orderid: 1564"

*

And now my problem is that these numbers (1,3,7) in the JSON file are random and there may be many orders. Is there any way for me to indicate a range of numbers, for example, 1-1000, and in that case if the result is not null to do the same?

I mean, to avoid having to create 1000 classes like class _1

2
  • 1
    Are you in control of how this JSON is created? In that case don't use integer as key name. What does that even represent? Why not use array of orders like orders: [{..}, {..}, {..}] where each {..} represents separate { "idOrder": ???, "price": ???}? Commented Sep 19, 2018 at 20:25
  • @Pshemo Hi, I do not have access to the creation of the JSON file, and thats my problem, beacuse it is not an array with []. Commented Sep 19, 2018 at 20:28

2 Answers 2

3

Change orders to a Map (HashMap)

private Map<String, Orders> orders;

And Orders to:

public class Orders {

    private String idOrder;
    private String price;

}

This should parse the Json as expected.

This question has a simular approach, but not using the class mapping in the example

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

Comments

0

Why not to do something like this:

public class MyJson {
    private int error;
    private Map<String, Order> orders;
    // getters and setters
}

With the Order defined so:

public class Order {
    private String idOrder;
    private String price;
    // getters and setters
}

And main method will look so:

public static void main(String[] args) throws FileNotFoundException {
    Gson gson = new Gson();
    BufferedReader br = new BufferedReader(new FileReader("base.json"));
    MyJson jsonOffline = gson.fromJson(br, MyJson.class);
    System.out.println("1 - Orderid: " + jsonOffline.getOrders().get("1").getIdOrder());
}

2 Comments

Awosome, thanks. So I made the next code lines: for (int i = 1; i <= 10; ++i) { String j = Integer.toString(i); Object obj = jsonOffline.getOrders().get(j).getIdOrder(); if (obj == null) { System.out.println("Order "+ i + ": does not exist"); } else { System.out.println("IdOrder " + i + ": " + jsonOffline.getOrders().get(j).getIdOrder()); } } It works with first order, and then get this error: Exception in thread "main" java.lang.NullPointerException
@Daniel, please replace second line in the for body with: Object obj = jsonOffline.getOrders().get(j);

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.