4

Here is my json Object.

{"id":"mrbbt6f3fa99gld0m6n52osge0",
 "name_value_list":
 {"user_default_dateformat":{"name":"user_default_dateformat","value":"m/d/Y"}},
 "module_name":"Users"}    

I got id,and module_name through following code.How can i get user_default_dateformat?.

I know it may so simple but I am a newbie in json.

        String jsonResponse;

        while ((jsonResponse = br.readLine()) != null) {

            jsonOutput = jsonResponse;

        }
        JSONObject job = new JSONObject(jsonOutput);

        System.out.println(job);// i can see the same json object 
                                                that i showen above.
        sessionID = job.get("id").toString();

Exception generating coge

JSONObject job2=new JSONObject(job);
dateFormat = job2.get("user_default_dateformat").toString();

The Eexception is

org.json.JSONException: JSONObject["user_default_dateformat"] not found.    

Thanks,

2 Answers 2

4

name_value_list is also an Object.

JSONObject job2 = new JSONObject(job.get("name_value_list"));

So there you get

job2.get("user_default_dateformat");

Every {} in your JSON is an object. So for every String you get which is something like {"xy":"za","ab":"cd"} you have to cast it to the JSONObject

Edit for your error:

As you can see in your code the line:

JSONObject job2=new JSONObject(job);

will try to generate a JSONObject out of your JSONObject.

You have to get the JSONObject in your JSONObject.

You want to get the user_default_dateformat which is in your JSONObject:

String name_value_list_string = job.get("name_value_list").toString();
//this string is another json-string which contains the user_default_dateformat
JSONObject name_value_list_object = new JSONObject(name_value_list_string);

//This JSONObject contains the user_default_dateformat but this is also a JSONObject

String user_default_dateformat_string = name_value_list_object.get("user_default_dateformat").toString();
//this String contains the user_default_dateformat JSONString

JSONObject user_default_dateformat_object = new JSONObject(user_default_dateformat_string);

//This JSONObject contains the String values of your user_default_dateformat
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you Loki..but i got the same execption..org.json.JSONException: JSONObject["user_number_seperator"] not found.
Can you post your code? Maybe I can see what your problem is. Just edit your Question and add your code.
Where did "user_number_seperator" come from? Are you trying to get a JSON property that's not in the JSON object?
So where is your problem? What does the stacktrace say? In what line occurc what error?
I got id with out any problem..but while am trying user_currency_name,i got this execption:org.json.JSONException: JSONObject["user_default_dateformat"] not found.
|
1

if you are using JSONSimple library you can use this:

jsonObject = (JSONObject) new JSONParser().parse(jsonstr); System.out.println((JSONObject)jsonObject.get("name_value_list"))).get("user_default_dateformat"));

This should give you the required result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.