Reputation: 19250
I want to parse a JSON string:
MyJsonString:
{
"status": "ok",
"count": 2,
"count_total": 9,
"pages": 5,
"posts": [
{
"id": 432,
"type": "post",
"title": "Title 1"
},
{
"id": 434,
"type": "post",
"title": "Title 2"
}
]
}
I have gone through:
http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
The examples work fine,but for that,i edited the JSON string to make it a Java String.
Ex: JSON String:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
I edited to:
String jsonStr = "{menu: {" +
"id: file," +
"value: File," +
"popup: {" +
"menuitem: [" +
"{value: New, onclick: CreateNewDoc()}," +
"{value: Open, onclick: OpenDoc()}," +
"{value: Close, onclick: CloseDoc()}" +
"]" +
"}" +
"}}";
But when i try to parse this myJsonString after accordingly editing it to be a valid Java String and run the project,it gives me warning and it does not display the Toast message which gives me titles.
Logcat:
10-19 18:36:45.972: WARN/System.err(1250): org.json.JSONException: Unterminated object at character 101 of { status : ok ,count : 2,count_total : 9,pages : 5,posts : [{id : 432,type : post ,title : Title 1 ,},{id : 434,type : post ,title : Title 2 ,},]}
I don't know where I am doing wrong? Even I have no idea,how to make Json String to a valid Java String programatically?
Any help appreciated.
Edit:
String jsonString="{\" status : ok \",\"count : 2\",\"count_total : 9\",\"pages : 5\",\"posts\" : [{\" id\" : \"432\",\"type\": \" post\", \"title\" : \" Title 1 \"},{ \"id \": \"434\",\type : post ,\"title\" : \" Title 2\"}]}";
JSONObject jsonObj = new JSONObject(jsonString);
String status_value = jsonObj.getString("status");
Toast.makeText(context,"Status_value= "+status_value, Toast.LENGTH_LONG).show();
I tried to toast status value this way.But I can't. Please help.
Upvotes: 0
Views: 1289
Reputation: 67286
In your case the response of the JSON coming is not a valid one.
"count": 2, which is not the correct way it should be in double quotes like this
"count": "2", same way for the rest of the response String also.
UPDATED:
You exact JSONString that you have created is wrong, just replace my string and checkout.
First String
String jsonString = "{\"status\": \"ok\",\"count\": \"2\",\"count_total\": \"9\",\"pages\": \"5\",\"posts\":" +
"[{\"id\": \"432\",\"type\": \"post\",\"title\": \"Title 1\"}," +
"{\"id\": \"434\",\"type\": \"post\",\"title\": \"Title 2\"}]}";
Second String
String jsonStr = "{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": " +
"[{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}," +
"{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
Upvotes: 3
Reputation: 12135
The string in your java code is NOT a valid JSON string. you do not enclode the strings with quotes. try this one:
String example = "[\"a\", \"b\", \"c\"]";
This should give you a string array.
Upvotes: 3