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

            Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

            밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

            1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴