How to get field as string regardless of it's content type in Java?How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?How do I read / convert an InputStream into a String in Java?How do I create a Java string from the contents of a file?How do I generate random integers within a specific range in Java?What is the correct JSON content type?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How to split a string in JavaHow do I convert a String to an int in Java?

Is there a legal way for US presidents to extend their terms beyond two terms of four years?

How to define a macro inside a macro via if?

Why were the first airplanes "backwards"?

How did researchers find articles before the Internet and the computer era?

Copy group of files (Filename*) to backup (Filename*.bak)

Who voices the character "Finger" in The Fifth Element?

What game is this character in the Pixels movie from?

Why was Mal so quick to drop Bester in favour of Kaylee?

Sharing referee/AE report online to point out a grievous error in refereeing

Most important new papers in computational complexity

How to properly say asset/assets in German

How Do I Know When I am in Private Mode?

Does a return economy-class seat between London and San Francisco release 5.28 tonnes of CO2 equivalents?

How is this practical and very old scene shot?

Which is better for keeping data: primary partition or logical partition?

Different budgets within roommate group

If two black hole event horizons overlap (touch) can they ever separate again?

What verb for taking advantage fits in "I don't want to ________ on the friendship"?

How do I organize members in a struct to waste the least space on alignment?

How do I tell the reader that my character is autistic in Fantasy?

How to describe POV characters?

How can my story take place on Earth without referring to our existing cities and countries?

I just started should I accept a farewell lunch for a coworker I don't know?

Losing queen and then winning the game



How to get field as string regardless of it's content type in Java?


How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?How do I read / convert an InputStream into a String in Java?How do I create a Java string from the contents of a file?How do I generate random integers within a specific range in Java?What is the correct JSON content type?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How to split a string in JavaHow do I convert a String to an int in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to retrieve a field from a JsonObject as String. This field "agentid" sometimes come as long and sometimes as String. When it comes as a long, I get an exception




java.lang.ClassCastException: java.lang.Double cannot be cast to
java.lang.CharSequence




Following is the statement which hits this exception:



// get agent ID
String agentID = parameterJson.getString("agentid");


How to safely get the content of this field "agentid" and store it as String in Java?










share|improve this question






















  • You can use Double.toString(parameterJson.getDouble("agentid"); to store this as a String. However, you'll have to fit the method the the type of the object you want to parse.

    – Maxouille
    Mar 25 at 14:02

















1















I am trying to retrieve a field from a JsonObject as String. This field "agentid" sometimes come as long and sometimes as String. When it comes as a long, I get an exception




java.lang.ClassCastException: java.lang.Double cannot be cast to
java.lang.CharSequence




Following is the statement which hits this exception:



// get agent ID
String agentID = parameterJson.getString("agentid");


How to safely get the content of this field "agentid" and store it as String in Java?










share|improve this question






















  • You can use Double.toString(parameterJson.getDouble("agentid"); to store this as a String. However, you'll have to fit the method the the type of the object you want to parse.

    – Maxouille
    Mar 25 at 14:02













1












1








1








I am trying to retrieve a field from a JsonObject as String. This field "agentid" sometimes come as long and sometimes as String. When it comes as a long, I get an exception




java.lang.ClassCastException: java.lang.Double cannot be cast to
java.lang.CharSequence




Following is the statement which hits this exception:



// get agent ID
String agentID = parameterJson.getString("agentid");


How to safely get the content of this field "agentid" and store it as String in Java?










share|improve this question














I am trying to retrieve a field from a JsonObject as String. This field "agentid" sometimes come as long and sometimes as String. When it comes as a long, I get an exception




java.lang.ClassCastException: java.lang.Double cannot be cast to
java.lang.CharSequence




Following is the statement which hits this exception:



// get agent ID
String agentID = parameterJson.getString("agentid");


How to safely get the content of this field "agentid" and store it as String in Java?







java json






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 14:00









user3243499user3243499

9792 gold badges13 silver badges29 bronze badges




9792 gold badges13 silver badges29 bronze badges












  • You can use Double.toString(parameterJson.getDouble("agentid"); to store this as a String. However, you'll have to fit the method the the type of the object you want to parse.

    – Maxouille
    Mar 25 at 14:02

















  • You can use Double.toString(parameterJson.getDouble("agentid"); to store this as a String. However, you'll have to fit the method the the type of the object you want to parse.

    – Maxouille
    Mar 25 at 14:02
















You can use Double.toString(parameterJson.getDouble("agentid"); to store this as a String. However, you'll have to fit the method the the type of the object you want to parse.

– Maxouille
Mar 25 at 14:02





You can use Double.toString(parameterJson.getDouble("agentid"); to store this as a String. However, you'll have to fit the method the the type of the object you want to parse.

– Maxouille
Mar 25 at 14:02












3 Answers
3






active

oldest

votes


















1














Depnding on which JsonObject class you are using, I'm assuming it has a get() or getObject() method that returns an Object. You can call that method followed by .toString().



For example:



String agentID = parameterJson.getObject("agentid").toString();





share|improve this answer























  • It am getting java.lang.Double cannot be cast to io.vertx.core.json.JsonObject. Used code as you have suggested String agentID = parameterJson.getJsonObject("agentid").toString();

    – user3243499
    Mar 25 at 14:25












  • @user3243499 Perhaps you are not calling the right method. I don't know which class you are using, so I can't check it myself, but see if there's a method that returns an Object (not JsonObject).

    – Eran
    Mar 25 at 14:27


















1














There are different things you can do:



For the first you can do Double.toString(...) because the Exception tells you why the error appears.



Also you can do String.valueOf(Object) that can also handle every object you pass in there or you just use the toString method when you get an Object from the JSON.



You also are able to just use the getDouble instead of getString and use the type double.






share|improve this answer






























    0














    you can use this to convert it to string.



    String agentID = String.valueOf(parameterJson.getString("agentid"));





    share|improve this answer























    • parameterJson.getString("agentid") already returns a String, and throws an exception if the value of the given key is not a String.

      – Eran
      Mar 25 at 14:05











    • This wouldn't work as you're doing a getString(Double), it will still throw the exception, either String.valueOf(Object) or Object.toString() are the way to go

      – Oscar Arranz
      Mar 25 at 14:07













    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%2f55339550%2fhow-to-get-field-as-string-regardless-of-its-content-type-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









    1














    Depnding on which JsonObject class you are using, I'm assuming it has a get() or getObject() method that returns an Object. You can call that method followed by .toString().



    For example:



    String agentID = parameterJson.getObject("agentid").toString();





    share|improve this answer























    • It am getting java.lang.Double cannot be cast to io.vertx.core.json.JsonObject. Used code as you have suggested String agentID = parameterJson.getJsonObject("agentid").toString();

      – user3243499
      Mar 25 at 14:25












    • @user3243499 Perhaps you are not calling the right method. I don't know which class you are using, so I can't check it myself, but see if there's a method that returns an Object (not JsonObject).

      – Eran
      Mar 25 at 14:27















    1














    Depnding on which JsonObject class you are using, I'm assuming it has a get() or getObject() method that returns an Object. You can call that method followed by .toString().



    For example:



    String agentID = parameterJson.getObject("agentid").toString();





    share|improve this answer























    • It am getting java.lang.Double cannot be cast to io.vertx.core.json.JsonObject. Used code as you have suggested String agentID = parameterJson.getJsonObject("agentid").toString();

      – user3243499
      Mar 25 at 14:25












    • @user3243499 Perhaps you are not calling the right method. I don't know which class you are using, so I can't check it myself, but see if there's a method that returns an Object (not JsonObject).

      – Eran
      Mar 25 at 14:27













    1












    1








    1







    Depnding on which JsonObject class you are using, I'm assuming it has a get() or getObject() method that returns an Object. You can call that method followed by .toString().



    For example:



    String agentID = parameterJson.getObject("agentid").toString();





    share|improve this answer













    Depnding on which JsonObject class you are using, I'm assuming it has a get() or getObject() method that returns an Object. You can call that method followed by .toString().



    For example:



    String agentID = parameterJson.getObject("agentid").toString();






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 25 at 14:02









    EranEran

    301k39 gold badges512 silver badges586 bronze badges




    301k39 gold badges512 silver badges586 bronze badges












    • It am getting java.lang.Double cannot be cast to io.vertx.core.json.JsonObject. Used code as you have suggested String agentID = parameterJson.getJsonObject("agentid").toString();

      – user3243499
      Mar 25 at 14:25












    • @user3243499 Perhaps you are not calling the right method. I don't know which class you are using, so I can't check it myself, but see if there's a method that returns an Object (not JsonObject).

      – Eran
      Mar 25 at 14:27

















    • It am getting java.lang.Double cannot be cast to io.vertx.core.json.JsonObject. Used code as you have suggested String agentID = parameterJson.getJsonObject("agentid").toString();

      – user3243499
      Mar 25 at 14:25












    • @user3243499 Perhaps you are not calling the right method. I don't know which class you are using, so I can't check it myself, but see if there's a method that returns an Object (not JsonObject).

      – Eran
      Mar 25 at 14:27
















    It am getting java.lang.Double cannot be cast to io.vertx.core.json.JsonObject. Used code as you have suggested String agentID = parameterJson.getJsonObject("agentid").toString();

    – user3243499
    Mar 25 at 14:25






    It am getting java.lang.Double cannot be cast to io.vertx.core.json.JsonObject. Used code as you have suggested String agentID = parameterJson.getJsonObject("agentid").toString();

    – user3243499
    Mar 25 at 14:25














    @user3243499 Perhaps you are not calling the right method. I don't know which class you are using, so I can't check it myself, but see if there's a method that returns an Object (not JsonObject).

    – Eran
    Mar 25 at 14:27





    @user3243499 Perhaps you are not calling the right method. I don't know which class you are using, so I can't check it myself, but see if there's a method that returns an Object (not JsonObject).

    – Eran
    Mar 25 at 14:27













    1














    There are different things you can do:



    For the first you can do Double.toString(...) because the Exception tells you why the error appears.



    Also you can do String.valueOf(Object) that can also handle every object you pass in there or you just use the toString method when you get an Object from the JSON.



    You also are able to just use the getDouble instead of getString and use the type double.






    share|improve this answer



























      1














      There are different things you can do:



      For the first you can do Double.toString(...) because the Exception tells you why the error appears.



      Also you can do String.valueOf(Object) that can also handle every object you pass in there or you just use the toString method when you get an Object from the JSON.



      You also are able to just use the getDouble instead of getString and use the type double.






      share|improve this answer

























        1












        1








        1







        There are different things you can do:



        For the first you can do Double.toString(...) because the Exception tells you why the error appears.



        Also you can do String.valueOf(Object) that can also handle every object you pass in there or you just use the toString method when you get an Object from the JSON.



        You also are able to just use the getDouble instead of getString and use the type double.






        share|improve this answer













        There are different things you can do:



        For the first you can do Double.toString(...) because the Exception tells you why the error appears.



        Also you can do String.valueOf(Object) that can also handle every object you pass in there or you just use the toString method when you get an Object from the JSON.



        You also are able to just use the getDouble instead of getString and use the type double.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 14:04









        CodeMatrixCodeMatrix

        1,8541 gold badge11 silver badges22 bronze badges




        1,8541 gold badge11 silver badges22 bronze badges





















            0














            you can use this to convert it to string.



            String agentID = String.valueOf(parameterJson.getString("agentid"));





            share|improve this answer























            • parameterJson.getString("agentid") already returns a String, and throws an exception if the value of the given key is not a String.

              – Eran
              Mar 25 at 14:05











            • This wouldn't work as you're doing a getString(Double), it will still throw the exception, either String.valueOf(Object) or Object.toString() are the way to go

              – Oscar Arranz
              Mar 25 at 14:07















            0














            you can use this to convert it to string.



            String agentID = String.valueOf(parameterJson.getString("agentid"));





            share|improve this answer























            • parameterJson.getString("agentid") already returns a String, and throws an exception if the value of the given key is not a String.

              – Eran
              Mar 25 at 14:05











            • This wouldn't work as you're doing a getString(Double), it will still throw the exception, either String.valueOf(Object) or Object.toString() are the way to go

              – Oscar Arranz
              Mar 25 at 14:07













            0












            0








            0







            you can use this to convert it to string.



            String agentID = String.valueOf(parameterJson.getString("agentid"));





            share|improve this answer













            you can use this to convert it to string.



            String agentID = String.valueOf(parameterJson.getString("agentid"));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 25 at 14:03









            Onkar MusaleOnkar Musale

            7314 silver badges21 bronze badges




            7314 silver badges21 bronze badges












            • parameterJson.getString("agentid") already returns a String, and throws an exception if the value of the given key is not a String.

              – Eran
              Mar 25 at 14:05











            • This wouldn't work as you're doing a getString(Double), it will still throw the exception, either String.valueOf(Object) or Object.toString() are the way to go

              – Oscar Arranz
              Mar 25 at 14:07

















            • parameterJson.getString("agentid") already returns a String, and throws an exception if the value of the given key is not a String.

              – Eran
              Mar 25 at 14:05











            • This wouldn't work as you're doing a getString(Double), it will still throw the exception, either String.valueOf(Object) or Object.toString() are the way to go

              – Oscar Arranz
              Mar 25 at 14:07
















            parameterJson.getString("agentid") already returns a String, and throws an exception if the value of the given key is not a String.

            – Eran
            Mar 25 at 14:05





            parameterJson.getString("agentid") already returns a String, and throws an exception if the value of the given key is not a String.

            – Eran
            Mar 25 at 14:05













            This wouldn't work as you're doing a getString(Double), it will still throw the exception, either String.valueOf(Object) or Object.toString() are the way to go

            – Oscar Arranz
            Mar 25 at 14:07





            This wouldn't work as you're doing a getString(Double), it will still throw the exception, either String.valueOf(Object) or Object.toString() are the way to go

            – Oscar Arranz
            Mar 25 at 14:07

















            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%2f55339550%2fhow-to-get-field-as-string-regardless-of-its-content-type-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

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

            Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?