0

I have a java String variable newTempTT which holds the following:

myObject = {"VehicleID":"KL-9876","VehicleType":"Nissan","VehicleOwner":"Sanjiva"}

I would like to add this into a json object so i performed the following line of code:

JSONObject jObject = new JSONObject(newTempTT);

There is no error message but when i run the program it says

"of type java.lang.String cannot be converted to JSONObject"

What changes do I need to make in order to convert this string to a JSONObject. Thanks in advance.

3 Answers 3

1

From the javadoc:

    Parameters: source - `A string beginning with { (left brace) and ending with } (right brace).` 
    Throws:
    JSONException - If there is a syntax error in the source string or a duplicated key.

Check if you are following this currently.

Sign up to request clarification or add additional context in comments.

Comments

1

You seem to be encoding things to JSON yourself. Don't do this, let the library do it for you.

Comments

0

Include escape characters like this for every double quotes.

 String newTempTT = "myObject = {\"VehicleID\":\"KL-9876\",\"VehicleType\":\"Nissan\",\"VehicleOwner\":\"Sanjiva\"}";

JSONObject jObject = new JSONObject(newTempTT);

3 Comments

could you take a look at stackoverflow.com/questions/14088119/… and see if im passing a valid json string from my web service please
The thing is I don't know the asp.net and it's going over my head. But your problem seems minor to me. Are you sure you are getting correct json string from your asp side?
no friend, i think im getting the wrong json. I will look into it, thanks.

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.