Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:17:53+00:00 2026-06-18T06:17:53+00:00

The format of my json object is: String jsonObjRecv = { response:{ respobj:{ id:<int>,

  • 0

The format of my json object is:

   String jsonObjRecv =  {
      "response":{
       "respobj":{
        "id":<int>,
        "number":<string>,
        "validated":<boolean>
         }
        },
        "status":"ok",
        "errors":null
        }

It works when code is:

        JSONObject jsonObjCont = new JSONObject(jsonObjRecv);
        String getString= jsonObjCont.toString(2);

In this case getString != null and I can receive data, but when I try to get nested data of JSON object as like:

        JSONObject jsonObjCont = new JSONObject(jsonObjRecv);
        JSONObject regNumber = jsonObjCont.getJSONObject("respobj");
        String number= regNumber.getString("number");

it dont work.

I tried to use GSON library, but it works when:

public String parse(String jsonObjRecv) {
    JsonElement jelement = new JsonParser().parse(jsonObjRecv);
    String result = jelement.toString();
    return result;

and don’t work :

public String parse(String jsonObjRecv) {
    JsonElement jelement = new JsonParser().parse(jsonObjRecv);
    JsonObject jobject = jelement.getAsJsonObject();
    jobject = jobject.getAsJsonObject("respobj");

    String result = jobject.get("number").toString();
    return result;

Where is my mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:17:55+00:00Added an answer on June 18, 2026 at 6:17 am

    The problem is you’re not accessing your JSON object correctly – it’s an object that contains a response object which contains a respobj object.

    Gson example follows. Note the comment in the code – you need to get the response object then get the respobj from it.

    public static void main( String[] args )
    {
        String jsonObjRecv =  "{\"response\":{\"respobj\":{\"id\":1,\"number\":\"22\",\"validated\":true}},\"status\":\"ok\",\"errors\":null}";
    
        JsonElement jelement = new JsonParser().parse(jsonObjRecv);
        JsonObject jobject = jelement.getAsJsonObject();
    
        // Here is where you're making an error. You need to get the outer
        // 'response' object first, then get 'respobj' from that.
        jobject = jobject.getAsJsonObject("response").getAsJsonObject("respobj");
    
        String result = jobject.get("number").getAsString();
    
        System.out.println(result);
    
    }
    

    Output:

    22

    Edit to add: Note I used getAsString() vs. toString() – if you use the latter you get the raw JSON which will incluse the quotes around the value (e.g. the output would be "22")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My C# web method uses JavaScriptSerializer to return a JSON object in string format
I started to use Json.NET to convert a string in JSON format to object
I want to convert date time object in java to json string in format
I m getting Time data in this format in an JSON Object. created_time: 2012-04-01T15:02:52+0000
I am taking a date from a JSON object in the format of 2012-12-31
What C-sharp type can I serialize to get JSON object with format name:[[1,2,3],[1,2,3],[1,2,3]] If
How to convert a rails Active Records object's id:name hash object to JSON format
I'm trying echo the contents of an object in a JSON format. I'm quite
I have a serialized json string in the format: [{\Version\:\8.63\,\Date\:\07\/11\/2011 00:00:00\,\Count\:213}, {\Version\:\1.0\,\Date\:\07\/11\/2011 00:00:00\,\Count\:1}, .........
How would you format a Json response to include nested, one to many related

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.