Question

Hey I have this JSONarray and i wonder how to get the values ProductName, description, and cost from the array.

I use the following method to get a value like for example quantity:

quantitybasket.setText(basket.getString("quantity"));

When I try to get productName the logcat says that ProductName does not contain any value.

What do i have to do to get those values?

[{"id":4,"quantity":2,"product":{"id":2,"productName":"Audi R8","description":"The best of Audi","cost":1000000.0,"rrp":1500000.0,"product_category":[{"id":2,"category":{"id":1,"categoryname":"Supercars"}},{"id":3,"category":{"id":2,"categoryname":"Sportscars"}}]}}]

I save every value in an Android TextView

TextView productbasket = (TextView) productsListItem
    .findViewById(R.id.product_name_basket);

TextView descriptionbasket = (TextView) productsListItem
    .findViewById(R.id.description_basket);

TextView costbasket = (TextView) productsListItem
    .findViewById(R.id.cost_basket);

TextView quantitybasket = (TextView) productsListItem
    .findViewById(R.id.quantity_basket);
Était-ce utile?

La solution

You can use Google gson . An excellent Java library to work with JSON.

find it here: https://code.google.com/p/google-gson/

Using gson library classes you can do something like this:

    String myJson = "[{\"id\":4,\"quantity\":2,\"product\":{\"id\":2,\"productName\":\"Audi R8\",\"description\":\"The best of Audi\",\"cost\":1000000.0,\"rrp\":1500000.0,\"product_category\":[{\"id\":2,\"category\":{\"id\":1,\"categoryname\":\"Supercars\"}},{\"id\":3,\"category\":{\"id\":2,\"categoryname\":\"Sportscars\"}}]}}]";
    JsonElement jsonElement = new JsonParser().parse(myJson);
    // This is because your json string is an array {}
    JsonArray myJsonAsArray = jsonElement.getAsJsonArray();
    // Now inside the json array, you get an object, so get it first from '0' index
    JsonObject mainObject = myJsonAsArray.get(0).getAsJsonObject();
    // Now you have a 'product' JsonObject inside the main object
    // You can directly get it by using String name
    JsonObject productObject = mainObject.getAsJsonObject("product");
    // Once you have 'Product' object, you can get individual elements by following
    String productName = productObject.get("productName").getAsString();
    // This will print out "Audi R8"
    System.out.println(productName);

    //You know how it works. Now get other individual elements of the product object by name

I hope this helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top