Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm getting error cannot convert String to json object ..

While I'm converting this String

{"user_id":   "user_id: 140" };

Error getting due to the format problem?

Thanks in advance..

share|improve this question
3  
you get the error because you have not a JSONArray but a JSONObject – blackbelt 1 hour ago
can u briefly tell me how can convert.. – Raj 1 hour ago
convert what??? – blackbelt 1 hour ago
I want to get user_id.. – Raj 1 hour ago
2  
{ "user_id": { "user_id": "140" } } with this json your code is good – blackbelt 1 hour ago
show 3 more comments

2 Answers

try this JsonObject object=new JsonObject(jsonString); your string will be converted to JsonObject.

share|improve this answer
{"user_id":   "user_id: 140" };

Please make sure that your json string is true.

Example JSON String:

[{"property":"value"}, {"property":"value"}, ...]

OR

{"property":[{"property":"value"}]}

OR

...

So may be your JSON String is not correct.

You can try with (Without ";" at the end of line)

{"user_id":"140"} 

If you assign user_id: 140 as value then:

{"user_id":"user_id:140"}

Please make sure that at the end of line have no ;

And, how to parse JSON String???

The first {} => Object, [] => Array

  • Object without name

    JSONObject jObject = new JSONObject(jsonString);

  • Object without name but in JSONArray

    JSONObject jObject = jsonArray.getJSONObject(index); // example: index = 0

  • Object with name and in JSONArray

    JSONObject jObject = jsonArray.getJSONObject("name_of_object");

  • JSONArray is like JSONObject & jsonArray above is an instance of JSONArray

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.