0

very simple java/json question.

I have the following test chunk of code. I can get the 1st element using the ".get()" by either index, or by the key. but I can't get any other elements by key...

The test dies, with nothing on the cmdline.. I'm assuming this is due to something not being correctly set within my env to display err results..

UPDATE:: OK.. it appears that the real issue is I don't know how to get an item, and to 1st determine what "type" it should be cast to. for the "nickname","name".. if I cast them as String.. I get the correct result..

So, how can one iterate through the key/value list of the json to determime how to correctly get each item??

The test code is:

import org.json.simple.JSONObject;
import org.json.simple.*;
//import org.json.simple.JSONValue;


public class asuH {

public static void main(String[] args){
    final String[] arguments = args;

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            try{

                String json_=arguments[0];


                //--get the page for the 1st and 2nd urls...

                //test the json input..
                System.out.println("asdfsfd \n");
                System.out.println(json_);

                //JSONObject obj=new JSONObject();
                //Object obj=JSONValue.parse(json_);

                String k9="{\"nickname\":null,\"num\":100,\"contact\":{\"phone\":\"123456\",\"zip\":\"7890\"},\"balance\":1000.21,\"is_vip\":true,\"name\":\"foo\"}";

                //JSONObject obj = (JSONObject)JSONValue.parse(json_);
                JSONObject obj = (JSONObject)JSONValue.parse(k9);

                System.out.print("11 \n");
                String fa = (String)obj.get("nickname");
                System.out.print(fa);
                System.out.print("22 \n");
                fa = (String)obj.get("contact");  //<< not working!!!
                System.out.println("22 cc\n");
                System.out.println(fa);

                String ttt=obj.toString();
                System.out.print(ttt);

                System.out.println("\n s4354455 \n");
                System.exit(0);


            } 
                catch (Exception ex) {}
                System.exit(0);
        }
    });
}

}

any thoughts/pointers are appreciated.

thanks

2 Answers 2

1

The value corresponding to property named contact is not a String. Use the appropriate getter method, and don't cast.

// snip...
String fa = obj.getString("nickname");
// snip...
JsonObject contact = obj.getObject("contact");
// and so on
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Matt.. Thanks for your reply.. I was just adding additional information on what I realized! But how do I determine what the "type" of the returned item is so I can properly manage it.. or is JsonObject able to essentially handle all the types? thanks
I do not understand your question. You need to know the structure of the JSON ahead of time. Do you understand JSON?
If you do System.out.println( obj.get("contact").getClass() ); it will print the type of that property. JSON only has a small number of "types" see json.org (Basically: object, list, string, number)
0

You can get field from your object and ask for it's type:

Object field = obj.get("field");
if (field instanceof JSONArray) {
    ...
} else if (field instanceof JSONObject) {
    ...
} else if (field instanceof Number) {
    ...
} else  {
    ...
}

You get the picture...

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.