0

While trying to understand and learn JSON i got a question:

{
"items": [
    {
        "id": "353453",
        "count": 2,
        "amount": 50
    },
    {
        "id": "3534534",
        "count": 1,
        "amount": 1.9
    },
    {
        "id": "23423423",
        "count": 2,
        "amount": 1.1
    },
    {
        "id": "2342234",
        "count": 3,
        "amount": 32.9
    }
],
"oid": 1000000009
}

Thats what i coded until now:

public class Test {

    public static void main(String... args) throws Exception {

        String json = "{\"items\":[{\"id\":\"353453\",\"count\":2,\"amount\":50.00},{\"id\":\"3534534\",\"count\":1,\"amount\":1.90},{\"id\":\"23423423\",\"count\":2,\"amount\":1.10},{\"ean\":\"2342234\",\"count\":3,\"amount\":32.90}],\"oid\":1000000009}";

   System.out.println(dojObject.getItems().get(0).getID());


    }

}

class DataOrderJson {

    private List<ItemDetails> items;
    private Long oid;

    public DataOrderJson() {

    }

    public Long getOid() {
        return oid;
    }

    public List<ItemDetails> getItems() {
        return items;
    }

    public void setOid(Long oid) {
        this.oid = oid;
    }

    public void setItems(List<ItemDetails> items) {
        this.items = items;
    }

    @Override
    public String toString() {
        return String.format("{items:%s,oid:%s}", items, oid);
    }

}

class ItemDetails {
    private String id;
    private Long count;
    private Float amount;

    public ItemDetails() {

    }

    public String getID() {
        return id;
    }

    public Long getCount() {
        return count;
    }

    public Float getAmount() {
        return amount;
    }

    public void setID(String id) {
        this.ean = ean;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public void setAmount(Float amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return String.format("{id:%s,count:%s,amount:%s}", id, count, amount);
    }

}

Output as expected: 353453

Now i want to check if an id is part of the json string. I have troubles to convert the lsit of json objects into a list of java objects. How to put the json objects into a list and iterate through this list?

JustTheAverageGirl
  • 2,973
  • 4
  • 17
  • 18
  • I personally don't use GSON but shouldn't it just do that for you? After parsing you check whether `id` is `null`, if it is, it wasn't part of the json string. – Thomas Mar 08 '13 at 15:14
  • See here : http://stackoverflow.com/questions/5554217/google-gson-deserialize-listclass-object-generic-type – Sotirios Delimanolis Mar 08 '13 at 15:14
  • Is it me or the JSON example he gives does not map it all to the Java code he shows after? – Adam Gent Mar 08 '13 at 15:19
  • Yes, my bad. Have 2 different json strings and accidently used the wrong for the json string at the beginning. Edited for the corret json string :) – JustTheAverageGirl Mar 08 '13 at 15:28

1 Answers1

0

you need to create twi java objects:

class Order {

    private List<Item> items;
    private Long orderid;
    Order(){}
}

and

Class Item {
    private String ean;
    private Long count;
    private Float amount;
    Item(){}
}

then just call:

 Order jobj = new Gson().fromJson(json, Order.class);

to be able to parse json, you need objects that represent the same property names as the json object have.

fmt.Println.MKO
  • 2,065
  • 1
  • 17
  • 25