Reputation: 1391
I want to parse JSON in android I can parse one response while I am unable to parse the other one.
1) I am able to parse the following Web Service Response
[
{
"id": "763",
"title": ".46 Magnum recipe",
"Calories": "189",
"Carbohydrates": "5.5 g"
},
{
"id": "332",
"title": "100 Miles per Hour recipe",
"Calories": "317",
"Carbohydrates": "24.8 "
},
{
"id": "6759",
"title": "12 Gauge Shottie recipe",
"Calories": "515",
"Carbohydrates": "27.5 "
},
{
"id": "6760",
"title": "136 recipe",
"Calories": "153",
"Carbohydrates": "22.7 "
},
{
"id": "8986",
"title": "151 Reasons recipe",
"Calories": "113",
"Carbohydrates": "14.9 "
},
{
"id": "6761",
"title": "18 Till You Die recipe",
"Calories": "81",
"Carbohydrates": "3.9 g"
},
{
"id": "333",
"title": "187 Cocktail recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "9031",
"title": "1968 recipe",
"Calories": "66",
"Carbohydrates": "12.3 "
},
{
"id": "6762",
"title": "20/20 Lemon recipe",
"Calories": "258",
"Carbohydrates": "4.8 g"
},
{
"id": "334",
"title": "2000 Flushes recipe",
"Calories": "112",
"Carbohydrates": "6.7 g"
}
]
2) This response can be parsed
[
{
"id": "2",
"title": "A Little Dinghy recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "4",
"title": "A Clockwork Tangerine recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "5",
"title": "Tiki Rum recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "6",
"title": "Absolute Monster recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "18",
"title": "Anchors Away recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "21",
"title": "Apple Jell-o Shots recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "24",
"title": "Applegasm recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "27",
"title": "Attempted Suicide recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "32",
"title": "Banana Grape Smoothie recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "39",
"title": "Beautiful Side Ride recipe",
"Calories": "",
"Carbohydrates": ""
}
]
I am getting data from server in both cases but response 2 can not be parsed and updated in listview
Below is the the code how I am parsing both responses
JSONArray jObject = new JSONArray(resultData);
for (int i = 0; i < jObject.length(); i++) {
JSONObject menuObject = jObject.getJSONObject(i);
String title = menuObject.getString("title");
String calories = menuObject.getString("Calories");
String carbohydrates = menuObject.getString("Carbohydrates");
if ((calories != null && calories.length() > 0)
&& (carbohydrates != null && carbohydrates.length() > 0)) {
String[] tokens = carbohydrates.split(" ");
pair.add(new MyStringPair(title, calories + "/" + tokens[0]));
}
}
LogCat
05-24 10:27:53.452: W/System.err(887): org.json.JSONException: End of input at character 0 of
05-24 10:27:53.552: W/System.err(887): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
05-24 10:27:53.562: W/System.err(887): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
05-24 10:27:53.562: W/System.err(887): at org.json.JSONArray.<init>(JSONArray.java:87)
05-24 10:27:53.562: W/System.err(887): at org.json.JSONArray.<init>(JSONArray.java:103)
05-24 10:27:53.562: W/System.err(887): at com.example.mixdrinksv1.MyStringPair.makeData(MyStringPair.java:49)
05-24 10:27:53.562: W/System.err(887): at com.example.mixdrinksv1.SortbyCalTab.onCreateView(SortbyCalTab.java:40)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-24 10:27:53.572: W/System.err(887): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-24 10:27:53.582: W/System.err(887): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-24 10:27:53.582: W/System.err(887): at android.os.Handler.handleCallback(Handler.java:605)
05-24 10:27:53.582: W/System.err(887): at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 10:27:53.582: W/System.err(887): at android.os.Looper.loop(Looper.java:137)
05-24 10:27:53.582: W/System.err(887): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-24 10:27:53.582: W/System.err(887): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 10:27:53.592: W/System.err(887): at java.lang.reflect.Method.invoke(Method.java:511)
05-24 10:27:53.602: W/System.err(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 10:27:53.612: W/System.err(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 10:27:53.612: W/System.err(887): at dalvik.system.NativeStart.main(Native Method)
05-24 10:27:53.741: D/dalvikvm(887): GC_CONCURRENT freed 314K, 5% free 9634K/10055K, paused 13ms+9ms
05-24 10:27:54.402: I/JSONResult(887): [{"id":"2","title":"A Little Dinghy recipe","Calories":"","Carbohydrates":""},{"id":"4","title":"A Clockwork Tangerine recipe","Calories":"","Carbohydrates":""},{"id":"5","title":"Tiki Rum recipe","Calories":"","Carbohydrates":""},{"id":"6","title":"Absolute Monster recipe","Calories":"","Carbohydrates":""},{"id":"18","title":"Anchors Away recipe","Calories":"","Carbohydrates":""},{"id":"21","title":"Apple Jell-o Shots recipe","Calories":"","Carbohydrates":""},{"id":"24","title":"Applegasm recipe","Calories":"","Carbohydrates":""},{"id":"27","title":"Attempted Suicide recipe","Calories":"","Carbohydrates":""},{"id":"32","title":"Banana Grape Smoothie recipe","Calories":"","Carbohydrates":""},{"id":"39","title":"Beautiful Side Ride recipe","Calories":"","Carbohydrates":""}]
Below is json data which I am getting from server and Log.
24 10:27:54.402: I/JSONResult(887): [{"id":"2","title":"A Little Dinghy recipe","Calories":"","Carbohydrates":""},{"id":"4","title":"A Clockwork Tangerine recipe","Calories":"","Carbohydrates":""},{"id":"5","title":"Tiki Rum recipe","Calories":"","Carbohydrates":""},{"id":"6","title":"Absolute Monster recipe","Calories":"","Carbohydrates":""},{"id":"18","title":"Anchors Away recipe","Calories":"","Carbohydrates":""},{"id":"21","title":"Apple Jell-o Shots recipe","Calories":"","Carbohydrates":""},{"id":"24","title":"Applegasm recipe","Calories":"","Carbohydrates":""},{"id":"27","title":"Attempted Suicide recipe","Calories":"","Carbohydrates":""},{"id":"32","title":"Banana Grape Smoothie recipe","Calories":"","Carbohydrates":""},{"id":"39","title":"Beautiful Side Ride recipe","Calories":"","Carbohydrates":""}]
What is wrong with the second response ?
Let me know if need more information.
Upvotes: 1
Views: 770
Reputation: 1391
The problem in the second JSON response was Calories
and carbohydrates
are empty, while I was empty String
check as below.
if ((calories != null && calories.length() > 0)
&& (carbohydrates != null && carbohydrates.length() > 0)) {
String[] tokens = carbohydrates.split(" ");
pair.add(new MyStringPair(title, calories + "/" + tokens[0]));
}
Upvotes: 1
Reputation: 12304
Try use this parser. It load every array to hashmap -> key and value. Try not use hardcoding. It could be bad habits.
private List<HashMap<String, String>> parseJSonArray(JSONArray array) {
// Container
List<HashMap<String, String>> returnArray = new ArrayList<HashMap<String, String>>();
try {
for (int i = 0; i < array.length(); i++) {
// Download json object
JSONObject jsonObject = array.getJSONObject(i);
// Prepare temp hashmap
HashMap<String, String> temporaryArray = new HashMap<String, String>();
// Operate on every keys
Iterator iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next().toString();
// Save data to temp container
temporaryArray.put(key, jsonObject.get(key).toString());
}
// Add to main container
returnArray.add(temporaryArray);
}
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
// Return ready List
return returnArray;
}
Then you can call your list by:
mylist.get(index of hashmap).get(get value by inputting your string key)
example
String s = mylist.get(0).get("title");
Upvotes: 1
Reputation: 1506
Store it in an array list and in the getView method set it accordingly using the get() method of the arraylist
Upvotes: 0