Parsing Json string Help required - android

{"sessid":"Vn1qRrhZWUQxaF4Vq3AS-TSaCHnDwgJa8dYtI7ER_Xs",
"session_name":"SESS7b83e2fe2bcfd6997077715ad8799a43",
"user_product":[{"id":"19","uid":"1","created":"1336803149","modified":"1336803149",
"subject":"Patras Bukhari",]}}
Blockquote i have this json in a sting and i want to fetch data from the arrayy user_product
i have tried alot but unable to do that . And my project is on Android.
help is appreciated in advance thanks

check following code
String a = {"sessid":"Vn1qRrhZWUQxaF4Vq3AS-TSaCHnDwgJa8dYtI7ER_Xs",
"session_name":"SESS7b83e2fe2bcfd6997077715ad8799a43",
"user_product":[{"id":"19","uid":"1","created":"1336803149","modified":"1336803149",
"subject":"Patras Bukhari",]}}
JSONObject json1 = new JSONObject(a);
JSONArray userProduct = json1.getJSONArray("user_product");
for(int i = 0; i < userProduct.length();i++){
JSONObject tempJSON = userProduct..getJSONObject(i);
String id = tempJSON.getString("id");
String uid = tempJSON.getString("uid");
String created = tempJSON.getString("created");
String modified = tempJSON.getString("modified");
String subject = tempJSON.getString("subject");
}
Hope this will solve your problem

Is the above JSON the real one you are using? If it was, the format is not valid. I guess you might encounter some exceptions working with it. Regardless to the validity, you can get user_product array by
JSONObject source = new JSONObject(your_source);
JSONArray userProducts = source.getJSONArray("user_product");
for(int i = 0; i < userProducts.length(); i++){
JSONObject product = userProducts.getJSONObject(i);
int uid = product.getInt("uid"); //get the uid as integer
}

Related

Parsing json code to a string in android using volley

i need help to parse this json code to actual strings using android volley. this is the json code:[{"name":"Tayo","0":"Tayo","thread_name":"Welcome","1":"Welcome","post":"Hi there,","2":"Hi there,","post_time":"Sunday","3":"Sunday"},{"name":"Pete","0":"Pete","thread_name":"Welcome","1":"Welcome","post":"Hi,am pete","2":"Hi,am pete","post_time":"Monday","3":"Monday"}].
I have tried other helps but not working. Thanks!
Remember:
if the .json content starts with { is considered as a Json Object.
if the .json content starts with [ is considered as a Json Array.
so you hava a JsonArray then you can parse your content like this way:
//Obtain the JsonArray
JSONArray jsonArray = new JSONArray(myJsonContent);
// Get the JsonObjects inside JsonArray
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonobject = jsonArray.getJSONObject(i);
}
// strData is the json data received.
JSONArray jsonArray = new JSONArray(strData);
for (int i=0; i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name");
String zero = jsonObject.optString("0");
String thread_name = jsonObject.optString("thread_name");
String one = jsonObject.optString("1");
String post = jsonObject.optString("post");
String two = jsonObject.optString("2");
String post_time = jsonObject.optString("post_time");
String three = jsonObject.optString("3");
//Just an example
arrayName.add(jsonObject.optString("name"));
}
You can even use array to store data instead of the string.

I am facing some problems with json parsing

{"success":"true",
"groups":[
{
"groupId":"c20f2353-1f13-4ea0-8283-ghhjc4dcc725251b",
"name":"hb",
"description":"hjj",
"image":null,
"membersCount":1,
"groupType":"chaddt",
"productCategeory":"bdfjgh",
"members":[
{
"memberId":"0031ea31-a71c-49f8ddbff6-8adaa310db02",
"memberName":"ddddsnta",
"contactId":"5a303564dd-2349-4cca-a190-f36f28ff54cb",
"contactName":"dssnta",
"role":"member"
}
],
}
]
}
This is my json.I am having some difficulty in parsing this.I am trying this solution.please suggest some ideas how to parse this type of json.Thanks for your answer
JSONArray jsonArray = jsonObject.getJSONArray("groups");
for (int i = 0; i < jsonArray.length(); i++) {
groupid = jsonArray.getJSONObject(i).getString("groupId");
String name = jsonArray.getJSONObject(i).getString("name");
String description =jsonArray.getJSONObject(i).getString("description");
String membersCount = jsonArray.getJSONObject(i).getString("membersCount");
String intrested = jsonArray.getJSONObject(i).getString("productCategeory");
JSONArray memberJsonArray = jsonArray.getJSONObject(i).getJSONArray("members");
for (int j = 0; j < memberJsonArray.length(); j++) {
String memberNamename = memberJsonArray.getJSONObject(j).getString("contactName");
String contactId = memberJsonArray.getJSONObject(j).getString("contactId");
String role = memberJsonArray.getJSONObject(j).getString("role");
GroupsDto groupDtoData = new GroupsDto();
groupDtoData.setGroupName(name);
groupDtoData.setGroupServerId(groupid);
System.out.println("groupid"+groupid);
groupDtoData.setGroupDescription(description);
groupDtoData.setProductCategory(intrested);
System.out.println("descr"+intrested);
groupDtoData.setGetmemberCount(membersCount);
groupDtoData.setGroupmembername(memberNamename);
System.out.println("membernames"+memberNamename);
groupDto.add(groupDtoData);
db.addGroups(groupDtoData);
}
This is my json.I am having some difficulty in parsing this.I am trying this solution.please suggest some ideas how to parse this type of json.Thanks for your answer
Your JSON response does not have any array name.
Instead of this :
JSONArray jsonArray = jsonObject.getJSONArray("groups");
Use This:
JSONArray jsonArray = jsonObject.getJSONArray(yourResponseStringHere);
It just needed to use Gson library and create corresponding class with your json file.
Gson : https://github.com/google/gson
Check this answer .It may help you fixing this.
JsonArray jarray=response.getJsonArray("groups");
for(int i=0;i<jarray.length();i++)
{
JsonObject jobj=jarray.getJsonObject(i);
String groupId=jobj.optString("groupId");
//do like this for all
//to get members
JsonArray jarray2=jobj.getJsonArray("members");
for(int j=0;j<jarray2.length();j++)
{
JsonObject jobj2=jarray2.getJsonObject(j);
String memberId=jobj2.optString("memberId");
//do this for all
}
}

Simple Json Example with android

I want to read JSON web service result.
My web service result is
[{"etkinlikTarihi":123,"eposta":"posta","etkinlikAdi":"ali","etkinlikDetay":"veli","etkinlikId":1},{"etkinlikTarihi":12,"eposta":"posta","etkinlikAdi":"ali","etkinlikDetay":"detay","etkinlikId":2},{"etkinlikTarihi":13,"eposta":"posta","etkinlikAdi":"mali","etkinlikDetay":"detay","etkinlikId":3},{"etkinlikTarihi":13,"eposta":"posta","etkinlikAdi":"mali","etkinlikDetay":"detay","etkinlikId":4}]
But, whenever I try to read with JSONObject in Android, I get a exception...
My code is here,
jsonResponse = new JSONObject(wsEtkinlikListesi);
String etkinlikTarihi = jsonResponse.getString("etkinlikTarihi");
And this is my stack trace:
org.json.JSONException: Value [{"etkinlikAdi":"toplanti","etkinlikDetay":"toplan","etkinlikTarihi":1454648400000,"etkinlikId":5,"eposta":"sefagenel#gmail.com"}] of type org.json.JSONArray cannot be converted to JSONObject
Where is my mistake and how can I correct it?
First try to parse the array and then get every object on it:
JSONArray arr = new JSONArray(jSonResultString);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonobject = arr.getJSONObject(i);
String name = jsonobject.getString("etkinlikTarihi");
String url = jsonobject.getString("posta");
...
}

JSONObject or JSONArray

I have data on this string, but I'm having trouble to access the individual data.
This is my code:
Log.d("Detail Outputss", "" + response.toString());
And this is the string output:
{"futsal_id":"45","info":[{"futsal_id":"45","futsal_name":"Kathmandu Futsal","city":"Kathmandu","address":"Kathmandu","owner_name":"Hari Prasad","owner_address":"Kathmandu","email":"kathmandufutsal#gmail.com","password":"kathmandu","phone_no":"1111111","mobile_no":"9841112233","status":"1"}],"description":[{"futsal_id":"45","futsal_desc":"Futsal is the fever which never ends.\r\nKathmandu futsal is - a platform, a medium of communication for our fraternity. Not just somewhere to host challenges or seek venues, but a place where we can all share and spread the luv! Here, you can find just about anything, do just about anything, and see just about anything. Unbelievable? Believe it."}],"features":[{"futsal_id":"45","futsal_feat":"Free Wifi"},{"futsal_id":"45","futsal_feat":"High Quality Grass"},{"futsal_id":"45","futsal_feat":"Canteen Facility"},{"futsal_id":"45","futsal_feat":"Friendly Environment"}],"dimension":[{"futsal_id":"45","dimension":"40m X 20m"}],"no_of_futsal":[{"futsal_id":"45","number":"1"}],"opening_hrs":[{"futsal_id":"45","open_time_id":"1","open_time":"6am","close_time_id":"15","close_time":"9pm"}],"price_weekdays_price1":[{"futsal_id":"45","price_id":"1","start_time":"6am","end_time":"12pm","price":"1200"}],"price_weekdays_price2":[{"futsal_id":"45","price_id":"2","start_time":"12pm","end_time":"6pm","price":"1000"}],"price_weekdays_price3":[{"futsal_id":"45","price_id":"3","start_time":"6pm","end_time":"9pm","price":"1500"}],"price_weekend_price1":[{"futsal_id":"45","price_id":"1","start_time":"6pm","end_time":"12pm","price":"1500"}],"price_weekend_price2":[{"futsal_id":"45","price_id":"2","start_time":"12pm","end_time":"6pm","price":"1800"}],"price_weekend_price3":[{"futsal_id":"45","price_id":"3","start_time":"6pm","end_time":"9pm","price":"2000"}],"phone_number_address":[{"futsal_id":"45","futsal_name":"Kathmandu Futsal","city":"Kathmandu","address":"Kathmandu","owner_name":"Hari Prasad","owner_address":"Kathmandu","email":"kathmandufutsal#gmail.com","password":"kathmandu","phone_no":"1111111","mobile_no":"9841112233","status":"1"}],"news":[{"futsal_id":"45","news_id":"7","news_title":"asdfasdfasdfasdf","news_description":"asdfashdflahsdlfasdjf;oajsd;ofjaosdijfoaisdjofajsdfja;sjdf;ajsd;fja;ksdjf;kasjd;fkja;sdjf;asjd;fajsd;fjasdjf;asjd;fjas;djf;asdf"},{"futsal_id":"45","news_id":"6","news_title":"awefasdf","news_description":"sdjf;asdf;a;sdf;asjdf;oajsd;fja;sdjf;oajsd;oifjaosdf"},{"futsal_id":"45","news_id":"5","news_title":"asdfasdf","news_description":"sadfasdfasdfasdfa"}]}
I want to get the value of each object futsal_id, futsal_name, city and others, thanks in advance!!
It is JSONArray within a JSONObject
JSONObject json = new JSONObject(response);
JSONArray info = json.getJSONArray("info");
//getting the first value.. loop it if you have more than one
JSONObject infoObject = info.getJSONObject(0);
JSONArray description = json.getJSONArray("description");
JSONObject json = new JSONObject(response);
int fustal_id =Integer.parseInt(json.getString("futsal_id"));
JSONArray inf = json.getJSONArray("info");
JSONObject info = inf.getJSONObject(0);
...
JSONObject responseObject = new JSONObject(response);
JSONArray info = responseObject.getJSONArray("info");
for(int i = 0; i < info.length(); i++) {
JSONObject obj = info.getJSONObject(i);
String futsal_id = obj.getString("futsal_id");
String futsal_name = obj.getString("futsal_name");
//so on
}
Be sure to catch the exceptions.

how to make JSObject as array?

I have alot of link to get data from web hence I wanted to use loop to retrieve each URL's data but I had trouble in making JSObject as array.
JSONObject[] jsObjectallnewstype;
JSONArray[] jsonArrayallnewstype = null;
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}
This line jsObjectallnewstype[i] get null error although the log shows JSONFunction.getnewstype successfully retrieve the data.
And i am also worry tat second line jsonArrayallnewstype[i] could cause same error as well.
So JSObject cant be put as array? If so what are the alternative??
To fix your current code, you need to initialize your array. This is why you are getting a NPE:
JSONObject[] jsObjectallnewstype = new JSONObject[categories.length()];
JSONArray[] jsonArrayallnewstype = new JSONArray[categories.length()];
for(int i = 0; i < categories.length(); i++)
{
JSONObject c = categories.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String url = c.getString(TAG_URL);
jsObjectallnewstype[i] = JSONFunction.getnewstype(title, url); //java.lang.NullPointerException
jsonArrayallnewstype[i] = jsobjectallnewstype[i].getJSONArray(TAG_NEWLIST);
}

Categories

Resources