Android json parsing error - android

My json data is given below:
[
{"Category_Id":"C1 ","Category_Name":"breakfast"},
{"Category_Id":"C2 ","Category_Name":"appetizers & snacks"},
{"Category_Id":"C3 ","Category_Name":"sandwiches & more"},
{"Category_Id":"C4 ","Category_Name":"burgers & wraps"},
{"Category_Id":"C5 ","Category_Name":"salads"},
{"Category_Id":"C6 ","Category_Name":"soup"},
{"Category_Id":"C7 ","Category_Name":"pizza & pasta"},
{"Category_Id":"C8 ","Category_Name":"kids menu"},
{"Category_Id":"C9 ","Category_Name":"main dishes"},
{"Category_Id":"C10 ","Category_Name":"oriental cuisine"},
{"Category_Id":"C11 ","Category_Name":"desserts"},
{"Category_Id":"C12 ","Category_Name":"shakes & smoothies"},
{"Category_Id":"C13 ","Category_Name":"drinks"},
{"Category_Id":"C14","Category_Name":"333"}
]
how could i parse these data and set the text in button dynamically.

String str ="[{\"Category_Id\":\"C1 \",\"Category_Name\":\"breakfast\"},{\"Category_Id\":\"C2 \",\"Category_Name\":\"appetizers & snacks\"},{\"Category_Id\":\"C3 \",\"Category_Name\":\"sandwiches & more\"},{\"Category_Id\":\"C4 \",\"Category_Name\":\"burgers & wraps\"},{\"Category_Id\":\"C5 \",\"Category_Name\":\"salads\"},{\"Category_Id\":\"C6 \",\"Category_Name\":\"soup\"},{\"Category_Id\":\"C7 \",\"Category_Name\":\"pizza & pasta\"},{\"Category_Id\":\"C8 \",\"Category_Name\":\"kids menu\"},{\"Category_Id\":\"C9 \",\"Category_Name\":\"main dishes\"},{\"Category_Id\":\"C10 \",\"Category_Name\":\"oriental cuisine\"},{\"Category_Id\":\"C11 \",\"Category_Name\":\"desserts\"},{\"Category_Id\":\"C12 \",\"Category_Name\":\"shakes & smoothies\"},{\"Category_Id\":\"C13 \",\"Category_Name\":\"drinks\"},{\"Category_Id\":\"C14\",\"Category_Name\":\"333\"}]";
JSONArray jarray;
try{
jarray = new JSONArray("");
for(int i = 0; i < jarray.length(); i++)
{
String id = jarray.getJSONObject(i).getString("Category_Id");
String name = jarray.getJSONObject(i).getString("Category_Name");
}
}catch(Exception e){}

If you get json parser error just check whether your json string is correct or not by checking it in online in any json parsing website like:-
http://jsonlint.com/
OR
http://jsonviewer.stack.hu/

For this all you need to do is a for loop to go through the Json response. Something like this should do the trick.
JSONArray jsonArray;
try{
jsonArray = new JSONArray(myJsonResponse);
for(int i = 0; i < jsonArray.length(); i++)
{
JsonObject object = new JsonObject(jsonArray.getJsonObject(i));
buttonTextString = object.getString("Category_Name);
}
} catch(Exception e) {}

You can parse JSONArray before then parse JSONObject to parse each element on your json data. And final setText for button each element you can parse

Related

How to parse json data and load it in the Listview

I am getting a json object from the api, I want to parse this json object and get the text value. I am using volley library and need to load it in listiew adapter.The json is object-array-object type.This is my Json object. Can you please help.
{"result":[{"id":"1","ExpendName":"rice","Cost":"500","Dates":"2018-01-09 11:13:58"},{"id":"2","ExpendName":"Meat","Cost":"550","Dates":"2018-01-09 11:17:27"},{"id":"3","ExpendName":"Fish","Cost":"250","Dates":"2018-01-09 11:21:30"},{"id":"4","ExpendName":"Pant","Cost":"700","Dates":"2018-01-09 11:36:45"},{"id":"5","ExpendName":"Shirt","Cost":"1000","Dates":"2018-01-09 11:50:11"},{"id":"6","ExpendName":"Tea","Cost":"55","Dates":"2018-01-09 13:37:42"},{"id":"7","ExpendName":"Lunch, Tea, Transport ","Cost":"750","Dates":"2018-01-09 13:41:29"},{"id":"8","ExpendName":"Breakfast ","Cost":"30","Dates":"2018-01-10 05:34:07"},{"id":"9","ExpendName":"Train","Cost":"460","Dates":"2018-01-11 05:32:42"},{"id":"10","ExpendName":"Bus","Cost":"1250","Dates":"2018-01-11 05:33:11"},{"id":"11","ExpendName":"Train","Cost":"500","Dates":"2018-01-11 10:03:00"}]}
You would normally parse the data this way:
try {
JSONObject jsonObject = new JSONObject(rawData);
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject mealObject = jsonArray.getJSONObject(i);
String id = mealObject.getString("id");
String expendName = mealObject.getString("ExpendName");
String cost = mealObject.getString("Cost");
String date = mealObject.getString("Dates");
//Do whatever you want with the data
}
} catch (JSONException e) {
e.printStackTrace();
}

How to read this JSON

String json =
[
{"a":{"aa":"string","ab":"string"} },
{"b":{"ba":"string","bb":"string"} }
]
I am trying to parse this data using JsonObject.
When i use this code :
JSONStringer Js= new JSONStringer(json);
Log.d("json", ""+Js);
it gives me this : (the first line of json but i want all data)
{"a":{"aa":"string","ab":"string"} }
How can I read this with Android ?
It is a JsonArray not a simple JsonObject
try this:
JSONArray a = new JSONArray(json);
for (int i = 0; i < a.length(); i++) {
JSONObject row = a.getJSONObject(i);
Log.d("json", ""+row);
}
Actually, this JSON is kind of weird, as it wraps the array objects into one additional object each ("a" and "b"), which will make it hard to parse. It should look like this:
String json = [ {"name":"a","aa":"string","ab":"string"},
{"name":"b","ba":"string","bb":"string"} ]
And then you would parse it like this:
JSONArray root = new JSONArray(jsonString); //this is the above string
for(int i = 0; i < root.length(); i++){
JSONObject current = root.getJSONObject(i);
//Do the parsing here
}

Parsing json array with no arrayobject name in android

I want to parse the following data, everything is done but i'm confused at this last stage:
["ABC","DEF","GHI"]
How can I parse this type of json data?
Do it like this
JSONArray arr = new JSONArray(data); // assuming your json array is store in data
for(int i = 0; i < arr.length(); i++)
{
String str = arr.getString(i); // will return one string at a time
}

Json data extracting in android

i want to extract the given json arry in android,
[{"Outofserviceday":{"outofservice":"2013-02-22"}},
{"Outofserviceday":{"outofservice":"2013-02-27"}},
{"Outofserviceday":{"outofservice":"2013-02-28"}}]
i have the code for extracting the json data like given below
[{"Requestcard":{"id":"994","userprofile_id":"14","userprofile_name":"Syed
Imran","company_name":"DLF Akruti Park, Hinjewadi, Pune,
Maharashtra","sex":"male","travel_date":"2013-02-12"}}]
in this case we can retrive the json boject using the code
JSONObject menuObject = json_data.getJSONObject("Requestcard");
and retrieve each element by
requestid= menuObject.getString("id");
But in the first case how we identify each Outofserviceday in the json array ? and How extract each data ???
you can do something like below, can create jsonArray from string json data and then can extracts json objects in a loop or so.
String json =" [{\"Outofserviceday\":{\"outofservice\":\"2013-02-22\"}}]"; //json-data which is basically a json array
JSONArray jArray = new JSONArray(json); / creating an jsonarray
for (int i = 0; i < jArray.length(); i++) {
// you can have jsonObject from json array here in the loop
}
Try this:
JSONObject json = new JSONObject(result);
JSONArray json1= json.getJSONArray("data");
if (json1.length()!=0) {
for (int i = 0; i < json1.length(); i++) {
String name = json1.getJSONObject(i).getString("name");
}
}
With the help of below code you can retrieve the value of outofservice
JSONArray jArray = new JSONArray(your data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jOutOfServiceDay = jArray.getJSONObject(i);
JSONObject jobj = jOutOfServiceDay.getJSONObject("Outofserviceday");
Log.i("Required data is:", "" + jobj.getString("outofservice"));
}

Android: Decoding JSON

[{"placeID":"p0001","placeName":"INTI International University","placeType":"Education","placeLat":"2.813997","placeLng":"101.758229","placePict":""},{"placeID":"p0002","placeName":"Nilai International College","placeType":"Education","placeLat":"2.814179","placeLng":"101.7700107","placePict":""}]
How do I decode the JSON sent from my PHP script on Android?
please try this
String s = "[{\"placeID\":\"p0001\",\"placeName\":\"INTI International University\",\"placeType\":\"Education\","
+ "\"placeLat\":\"2.813997\",\"placeLng\":\"101.758229\",\"placePict\":\"\"},"
+ "{\"placeID\":\"p0002\",\"placeName\":\"Nilai International College\",\"placeType\":\"Education\",\"placeLat\":\"2.814179\",\"placeLng\":\"101.7700107\",\"placePict\":\"\"}]";
ArrayList<String> arrplaceID = new ArrayList<String>();
ArrayList<String> arrplaceName = new ArrayList<String>();
try {
JSONArray arr = new JSONArray(s);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonObject = arr.getJSONObject(i);
arrplaceID.add(jsonObject.optString("placeID"));
arrplaceName.add(jsonObject.optString("placeName"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < arrplaceID.size(); i++) {
Log.e("arr[" + i + "] place Name", arrplaceName.get(i));
}
What is the problem in this Please Read this tutorial for parsing JSON it might be helpful in future also.json parsing link
Follow below points.
1) it seems the response you are getting is Json Array. so create one json array by response string.
JSonArray jArray = new JsonArray(responseString);
2) now you have your response in jArray. now iterate a loop and take json object from JsonArray, in your case you have two json objects.
for(i,i<jArray.size,i++)
{
JsonObject obj=jArray.get(i);
// here you got your first entry in jsonObject.
// nor use this obj according to ur need. you can say obj.getString("placeID");
// and so on.
}
refer this to understand more on json link
use JSONArray class:
JSONArray jsonplaces = new JSONObject(stringPlaces);
then your able to iterate throught array by using for-loop:
for (int i = 0; i < jsonplaces.length(); i++) {
JSONObject jsonplace = (JSONObject) jsonplaces.get(i);
//read items, for example:
String placeName = jsonplace.getString("placeName");
}

Categories

Resources