How to retrieve data from json object in java?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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









share|improve this question




















  • 1





    Possible duplicate of How to parse JSON in Java

    – Dang Nguyen
    Nov 29 '18 at 6: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 6: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 6:24













  • Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?

    – fervent
    Nov 29 '18 at 6: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 6:31


















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









share|improve this question




















  • 1





    Possible duplicate of How to parse JSON in Java

    – Dang Nguyen
    Nov 29 '18 at 6: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 6: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 6:24













  • Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?

    – fervent
    Nov 29 '18 at 6: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 6:31














0












0








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









share|improve this question
















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






java json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 9:26









Rai

1,1444823




1,1444823










asked Nov 29 '18 at 5:59









ferventfervent

287




287








  • 1





    Possible duplicate of How to parse JSON in Java

    – Dang Nguyen
    Nov 29 '18 at 6: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 6: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 6:24













  • Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?

    – fervent
    Nov 29 '18 at 6: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 6:31














  • 1





    Possible duplicate of How to parse JSON in Java

    – Dang Nguyen
    Nov 29 '18 at 6: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 6: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 6:24













  • Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?

    – fervent
    Nov 29 '18 at 6: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 6:31








1




1





Possible duplicate of How to parse JSON in Java

– Dang Nguyen
Nov 29 '18 at 6:03





Possible duplicate of How to parse JSON in Java

– Dang Nguyen
Nov 29 '18 at 6: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 6:20







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 6: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 6:24







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 6:24















Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?

– fervent
Nov 29 '18 at 6:25







Can you please edit the above code to retrieve the value of Camera i.e., 80000 ?

– fervent
Nov 29 '18 at 6: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 6:31





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 6:31












3 Answers
3






active

oldest

votes


















0














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





share|improve this answer































    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 {}.






    share|improve this answer































      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.






      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532730%2fhow-to-retrieve-data-from-json-object-in-java%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














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





        share|improve this answer




























          0














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





          share|improve this answer


























            0












            0








            0







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





            share|improve this answer













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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 29 '18 at 6:55









            RaiRai

            1,1444823




            1,1444823

























                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 {}.






                share|improve this answer




























                  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 {}.






                  share|improve this answer


























                    0












                    0








                    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 {}.






                    share|improve this answer













                    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 {}.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 29 '18 at 6:36









                    343GuiltySpark343GuiltySpark

                    1516




                    1516























                        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.






                        share|improve this answer




























                          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.






                          share|improve this answer


























                            0












                            0








                            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.






                            share|improve this answer













                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 29 '18 at 7:20









                            user2414557user2414557

                            97




                            97






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532730%2fhow-to-retrieve-data-from-json-object-in-java%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Contact image not getting when fetch all contact list from iPhone by CNContact

                                Idjassú

                                count number of partitions of a set with n elements into k subsets