0

I'm trying to retrieve Value i.e., 80000 where Name is "Camera" from the Product's JSON using java. Can anyone please help me out ?    

{
   "Products":{  
      "Product":[  
         {  
            "Name":"Tv",
            "Value":50000
         },
         {  
            "Name":"Camera",
            "Value":80000
         },
         {  
            "Name":"Phone",
            "Value":15000
         },
         
      ]
   }
}

Mycode:

JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
        JSONArray products = arrayOfProducts.getJSONArray("Product");
        for (int i = 0; i < products.length(); i++) {
            JSONObject objects = products.getJSONObject(i);
            Iterator key = objects.keys();
            while (key.hasNext()) {
                String k = key.next().toString();
                if(k.equals("Name")) {
                    if(objects.getString(k).equals("Camera")) {
                System.out.println("Key : " + k + ", value : " + objects.getString(k));
                    }
                }
            }
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
fervent
  • 123
  • 1
  • 2
  • 10
  • 1
    Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Dang Nguyen Nov 29 '18 at 06:03
  • Your code look normal. But your JSON is not good. Strange quotes and JSON object must begin with "{" and end with "}". How do you create jsonObj? What error do you see? P.S. Your can take "Name" like this: objects.getString("Name") and check "Name" existing (if you need) like this: objects.has("Name") – Anatoly Samoylenko Nov 29 '18 at 06:20
  • This is a part of json. jsonObj contains entire json through which I'm trying to parse the json. Edited quotes – fervent Nov 29 '18 at 06:24
  • Can you please edit the above code to retrieve the value of Camera i.e., 80000 ? – fervent Nov 29 '18 at 06:25
  • replace System.out.println("Key : " + k + ", value : " + objects.getString(k)); on System.out.println("Key : " + k + ", name : " + objects.getString(k) + ", value : " + objects.getInt("Value")); – Anatoly Samoylenko Nov 29 '18 at 06:31

3 Answers3

1

If you want to get the value in a key-value pair in a JSON you need to use the following syntax:

  • [JSONObject].get[data_type]([key_name]).

    In the above given syntax, replace the [JSONObject] with the variable of type JSONObject representing the JSON. In your case, it is objects.

    replace [data_type] with the data type of the value at that particular key. In the current case, value for the key "Value" is 80000 is an Integer.. Hence it should be getInt.

    Replace [key_name] with the key whose value you need to retrieve, which in your case is "Value".

Hence the code snippet you need to use to get the value 80000 which with the "Camera" part is: objects.getInt("Value").

Here is your overall updated code:

    JSONObject arrayOfProducts = jsonObj.optJSONObject("Products");
    JSONArray products = arrayOfProducts.getJSONArray("Product");
    for (int i = 0; i < products.length(); i++) {
        JSONObject objects = products.getJSONObject(i);
        Iterator key = objects.keys();
        while (key.hasNext()) {
            String k = key.next().toString();
            if(k.equals("Name")) {
                if(objects.getString(k).equals("Camera")) {
            System.out.println("\nKey : " + k + "\nName : " + objects.getString(k) + ", \nValue : " + objects.getInt("Value"));
                }
            }
        }
    }
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
0

Your JSON is wrong.

  • Don't use left/right quotes. Instead use quotes without a direction.

Correct: "Products" Wrong: “Products”

  • Each JSON object should be enclosed in {}. Enclose your whole JSON file content within {}.
343GuiltySpark
  • 123
  • 1
  • 2
  • 11
0

Why you need to choose complex solution to parse JSON like that?

You should use define some entities and then use library for reading JSON value (Ex: "ObjectMapper" of jackson) to parser it to Object.

You can leave all complex parts for them, after that you just working on java object.

user2414557
  • 19
  • 1
  • 9