2

my json look like this

{"FROM_JID":"ff2fbe2f1e856a0c5c93616a463f7bdeef9bf7a0","TO_JID":"30df65445882e2c83783aad963ae64ca9ab6891a","TYPE_ID":"1","PLATFORM":"IOS","CONTENT":"{\"FROM_JID\":\"ff2fbe2f1e856a0c5c93616a463f7bdeef9bf7a0\",\"FROM_HOST\":\"ssdevim.mtouche-mobile.com\",\"FROM_JNAME\":\"G0173A2D\",\"TO_JID\":\"30df65445882e2c83783aad963ae64ca9ab6891a\",\"TO_HOST\":\"ssdevim.mtouche-mobile.com\",\"MESSAGE_ID\":\"074f36be91093e9c1490a9d9c984e15d\",\"MESSAGE_TYPE\":\"not_avt\",\"STAMP\":\"2016-01-13T10:21:03.761817Z\",\"MESSAGE\":{\"mt\":\"not_avt\",\"jid\":\"ff2fbe2f1e856a0c5c93616a463f7bdeef9bf7a0@ssdevim.mtouche-mobile.com\"},\"BADGE\":14,\"CONTENT-AVAILABLE\":1,\"SOUND\":\"default\"}","DEVICE_ID":"54A89FDE-0981-46D2-895B-0445AFC4B072","PUSH_ID":"37257446ea74b2e9b688407aa8a6ad94dd270a9d450f37f2bc3f6c9af5362c69","CREATE_DATE":"2016-01-13","CREATE_TIME":"10:21:03"}

and my code is

JSONParser content_parser = new JSONParser();
                        Object objMessage_Content = content_parser.parse(strContent);
                        JSONObject jsonObjectMessage_Content = (JSONObject) objMessage_Content;
                        String strMT=(String) jsonObjectMessage_Content.get("MESSAGE_TYPE");
                        String strM=(String) jsonObjectMessage_Content.get("MESSAGE");

i got an exception that is

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to java.lang.String

and i checked json is valid json.can some one please help.

1
  • MESSAGE is an object not a String \"MESSAGE\":{...} Commented Jan 14, 2016 at 2:18

2 Answers 2

1

I am guessing that you are using gson

so first get the MESSAGE - this will be a JSONObject

JsonObject message = jsonObjectMessage_Content.getAsJsonObject("MESSAGE");

then you get the String MESSAGE_TYPE

message.getAsString ("MESSAGE_TYPE");
Sign up to request clarification or add additional context in comments.

Comments

0

You should use :

String strMT= jsonObjectMessage_Content.optString("MESSAGE_TYPE");
String strM= jsonObjectMessage_Content.optString("MESSAGE");

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.