Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I recently started working with andorid.
How to parse dynamic json with multiple arrays response from php server.
Login Response
{
"status": 1,
"data": {
"msg": "LoggedIn",
"user_id": "2"
}
}
Login response with error message
{
"status": 0,
"data": "No users found with given email."
}
and an other one
Inventory List
{
"status": 1,
"data": [
{
"inventory_id": "33",
"apron_id": "123456",
"nickname": "uyi",
"location": "13",
"manufacture": "0",
"garment_type": "yuyh",
"color": "juki",
"core_material": "ytyt",
"Date_purchase": "2015-04-10",
"UID_no": "ikujki",
"serial": "iui",
"Batch": "ikk",
"Expiration": "2015-04-23",
"QTY": "898",
"apron_status": "0",
"apron_retire": "0",
"created_user": "2",
"created_time": "2015-04-10 05:22:38",
"update_time": "2015-04-10 05:22:38"
},
{
"inventory_id": "32",
"apron_id": "12345mn",
"nickname": "gfhgh",
"location": "12",
"manufacture": "0",
"garment_type": "hgjyhj",
"color": "ytgtfghtg",
"core_material": "fhgfhy",
"Date_purchase": "2015-04-28",
"UID_no": "rtryttttttttt",
"serial": "hfh",
"Batch": "rtrrtyy",
"Expiration": "2015-03-17",
"QTY": "7688",
"apron_status": "0",
"apron_retire": "0",
"created_user": "2",
"created_time": "2015-04-10 05:15:54",
"update_time": "2015-04-10 05:15:54"
}
]
}
Thanks in advance.
to get login response you can do like this
JSONObject jobj=new JSONObject(result.toString());
String status=jobj.getString("status");
if(status.equalsIgnoreCase("1"))
{
//login success
JSONObject Jdata=jobj.getJSONObject("data");
String Message=Jdata.getString("msg");
String UserId=Jdata.getString("user_id");
}
else
{
//failure
}
and for Inventory List you can do like this
JSONObject jobj=new JSONObject(result.toString());
JSONArray arrData=jobj.getJSONArray("data");
for (int i = 0; i < arrData.length(); i++)
{
JSONObject jdata=arrData.getJSONObject(i);
//here u can get all field like this
String nickname=jdata.getString("nickname");
}
Investigate the JSONObject and JSONArray classes.
You can parse extremely simply:
JSONObject json = new JSONObject(jsonString);
int status = json.getInt("status");
Using both an Array and an Object for your "data" key will make things irritating for you. You should think about using a different key, or another key that will dictate to you what you are reading.
Related
I created an app that stream video from server, on server side (PHP) I use this code to pass the MySQL query result via JSON array contain a result of video title size and views.
$query="select * from video_tble ;";
$result=mysqli_query($con,$query);
$row=mysqli_fetch_all($result,MYSQLI_ASSOC);
$respones_result=array();
$response_result=$row;
echo json_encode(array("server_response_result"=>$response_result));
But I don't know how to retrieve a multidimensional array using JSON object.
JSONObject jsonObject1=new JSONObject(json);
JSONArray jsonArray1=jsonObject1.getJSONArray("server_response_result");
JSONObject JO1=jsonArray1.getJSONObject(0);
String [] title =...?
If it was a name value pair then I follow this strategy:
String title1=JO1.getString("title");
But I don't know what to do on multidimensional array.
Hi you need a for loop like this
for(int i;i<jsonArray1.size();i++){
JSONObject JO1=jsonArray1.getJSONObject(i);
String title1=JO1.getString("title");
}
To parse this json
{
"server_response_result": [{
"videoid": "1",
"videotitle": "Metro Shoes",
"userid": "7",
"category": "Fashion",
"dou": "0000-00-00",
"rate": "3",
"vstatus": "A",
"vcount": "321",
"location": "adstreamer\\uploads\\ramesh#gmail.com\\1.mp4"
}, {
"videoid": "2",
"videotitle": "Lijn the bus",
"userid": "7",
"category": "App",
"dou": "0000-00-00",
"rate": "4",
"vstatus": "A",
"vcount": "145",
"location": "adstreamer\\uploads\\ramesh#gmail.com\\2.mp4"
}]
}
You have to do following steps (saying you have the server response in jsonObject.
ArrayList<SomeModel> modelList = new ArrayList<>();
JSONArray serverResponseResult = jsonObject.getJsonArray("server_response_result");
for (int i = 0; i < serverResponseResult.length(); i++) {
JSONObject result = serverResponseResult.get(i);
String videoId = result.getString("videoid");
String videoTitle = result.getString("videotitle");
...
// instead of using String, set the values in the model class
modelList.add(/* model */);
}
1.I am taking data from Local Database every 10 mins sending to web server.
2.after reading from Local database , make Full database data as Json format then send to Webserver from android.
I want to make Like this:
[
{
"$id": "2",
"Contact": "999",
"Lat": "465465",
"Long": "65465",
"Time": "654654",
"Msg": "1"
},
{
"$id": "3",
"Contact": "12131321",
"Lat": "3413132",
"Long": "54564",
"Time": "54654",
"Msg": "1"
}
]
Reading from Local Database:
List<LocationPOJO> val = dbconnectorForlocation.getAllvalues();
Log.i("MY data String ",val.toString());
for(int i=0;i<val.size();i++)
{
ImeiStringval = val.get(i).getIMEIString();
LatstringVal = val.get(i).getLatString();
LongStringVal = val.get(i).getLongString();
StatusStingVal = val.get(i).getStatusString();
TimeandDateVal=val.get(i).getTImeandDate();
}
Please,help me how to do this.
Try this
JSONObject json;
JSONArray jsonArray = new JSONArray();
for(int i=0;i<dbData.size;i++)
{
json= new JSONObject();
json.put("$id", dbData.get(i).getId());
json.put("Contact", dbData.get(i).getContact());
json.put("Lat", dbData.get(i).getLat());
json.put("Long", dbData.get(i).getLong());
json.put("Time", dbData.get(i).getTime());
json.put("Msg", dbData.get(i).getMsg());
jsonArray.put(json);
}
I am writing a Native Android App in which i am using PHP MYSQL to get data from server Using GET Request.
The way that the server sends me the result is as follows:
{
"result": true,
"error_message": "",
"response": [
{
"total_tasks": 8,
"tasks": [
{
"id": 8,
"name": "Hello Christine, call Mr. Dejan Mandic"
},
{
"id": 7,
"name": "Hello Christine, call Ms. Charente Douglas-Smith"
},
{
"id": 6,
"name": "Hello Christine, call Mrs. Alison Marshall"
},
{
"id": 5,
"name": "Hello Christine, call Mrs. Muna Crisp"
},
{
"id": 4,
"name": "Hello Christine, call Mr. Victor Crisp"
},
{
"id": 3,
"name": "Hello Christine, call Mrs. Kathy Dunn"
},
{
"id": 2,
"name": "Hello Christine, call Mr. Peter Dunn"
},
{
"id": 1,
"name": "Hello Christine, call Ms. Vanessa Allen"
}
]
}
]
}
I need to show the data from the array "respones" -> "tasks" (i.e id and name) in my List View in android and eventually when the user clicks in a particular row in ListView the information about that task is shown in another activity using the id. As the server is sending the data in multidimensional array, I am having problem getting the data in my ListView.
It would be a great help if someone could kindly help me out.
Thank You :)
JSONObject responseObject = new JSONObject(responseString);
boolean resultSuccess = responseObject.getBoolean("result");
if(resultSuccess) {
JSONArray responseArray = responseObject.getJSONArray("response");
JSONObject firstResponse = responseArray.getJSONObject(0);
JSONArray tasks = firstResponse.getJSONArray("tasks");
for(int i = 0; i < tasks.length(); i++){
JSONObject task = tasks.getJSONObject(i);
int id = task.getInt("id");
String name= task.getString("name");
}
}
You just need to navigate through the tree to parse your json although I had some comments on your JSON tree
Why do you have the response as an array? It's only one object
right?
The number of tasks is redundant data you already have the tasks and can infer the length from that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#Override
protected String doInBackground(String... params) {
try{
JSONParser jParser = new JSONParser();
String url="http://earlykid.com/android/?page=2";
IdArray=jParser.getJSONFromUrl(url);
Log.e("Fetch Data",IdArray.toString());
mLatestList.clear();
for(int i=0;i<IdArray.length();i++){
try{
JSONObject jObject;
mKids=new Kids();
jObject=IdArray.getJSONObject(i);
mKids.SetTotalPages(jObject.getString("totalItems"));
mKids.SetCurrentPage(jObject.getString("currentPage"));
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Log.e("log_tag", "Failed data was:\n" + IdArray);
}
}
}catch(Exception e){
}
return null;
}
#Override
protected void onPostExecute(String result) {
mProgress.dismiss();
}
When i fetch the data from this code.it shows me error.
Logcat here:-
error parsing data org.json.JSONException: Value {"totalItems":38,"currentPage":"2","items":[{"id":"Atb1lE9_wzk","title":"ABCD Alphabets Song - Songs for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/Atb1lE9_wzk\/hqdefault.jpg"},{"id":"UXeeSU0QNro","title":"The rich man and his sons story - Animated Stories for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/UXeeSU0QNro\/hqdefault.jpg"},{"id":"HmiyKDYrELk","title":"Here we go round the mulberry bush - Nursery Rhyme for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/HmiyKDYrELk\/hqdefault.jpg"},{"id":"9TLnCurMs5c","title":"Old Mac Donald had a farm - Nursery Rhymes for Kids","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/9TLnCurMs5c\/hqdefault.jpg"},{"id":"DPQ_5GR_MMo","title":"Five Little Monkeys jumping on the bed - Nursery Rhymes","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/DPQ_5GR_MMo\/hqdefault.jpg"},{"id":"CvwHp2xFlJw","title":"Rain Rain go away - Nursery Rhyme","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/CvwHp2xFlJw\/hqdefault.jpg"},{"id":"WEVA9iF6i3s","title":"I'm a little teapot Nursery Rhyme with lyrics","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/WEVA9iF6i3s\/hqdefault.jpg"},{"id":"TQHyRssAM5Y","title":"Ten little fingers ten little toes - Nursery Rhyme","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/TQHyRssAM5Y\/hqdefault.jpg"},{"id":"fDGOlmgF1NE","title":"Jingle Bells Christmas Song","category":"Education","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/fDGOlmgF1NE\/hqdefault.jpg"},{"id":"Y83fbhN6FBk","title":"Pussy Cat Pussy Cat where have you been? - Nursery Rhyme","category":"Film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/Y83fbhN6FBk\/hqdefault.jpg"},{"id":"UuqNHZEIwEI","title":"Thank you for the world so sweet - Kids Song","category":"Film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/UuqNHZEIwEI\/hqdefault.jpg"},{"id":"g0u1iWUmg8Q","title":"Ding dong bell - Nursery Rhyme","category":"Film","thumbnail":"http:\/\/www.earlykid.com\/android\/timthumb.php?w=600&h=330src=http:\/\/i1.ytimg.com\/vi\/g0u1iWUmg8Q\/hqdefault.jpg"}]
What you are getting is a JSONObject from this url http://earlykid.com/android/?page=2
You have this
IdArray=jParser.getJSONFromUrl(url); // wrong. you get a json object
{ represents a json object node
[ represents a json array node
Your json
{
"totalItems": 38,
"totalPages": 3,
"itemsPerPage": 12,
"currentPage": "2",
"items": [
{
"id": "Atb1lE9_wzk",
"category": "Education",
"title": "ABCD Alphabets Song - Songs for Kids",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/Atb1lE9_wzk/hqdefault.jpg"
},
{
"id": "UXeeSU0QNro",
"category": "Education",
"title": "The rich man and his sons story - Animated Stories for Kids",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/UXeeSU0QNro/hqdefault.jpg"
},
{
"id": "HmiyKDYrELk",
"category": "Education",
"title": "Here we go round the mulberry bush - Nursery Rhyme for Kids",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/HmiyKDYrELk/hqdefault.jpg"
},
{
"id": "9TLnCurMs5c",
"category": "Education",
"title": "Old Mac Donald had a farm - Nursery Rhymes for Kids",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/9TLnCurMs5c/hqdefault.jpg"
},
{
"id": "DPQ_5GR_MMo",
"category": "Education",
"title": "Five Little Monkeys jumping on the bed - Nursery Rhymes",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/DPQ_5GR_MMo/hqdefault.jpg"
},
{
"id": "CvwHp2xFlJw",
"category": "Education",
"title": "Rain Rain go away - Nursery Rhyme",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/CvwHp2xFlJw/hqdefault.jpg"
},
{
"id": "WEVA9iF6i3s",
"category": "Education",
"title": "I'm a little teapot Nursery Rhyme with lyrics",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/WEVA9iF6i3s/hqdefault.jpg"
},
{
"id": "TQHyRssAM5Y",
"category": "Education",
"title": "Ten little fingers ten little toes - Nursery Rhyme",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/TQHyRssAM5Y/hqdefault.jpg"
},
{
"id": "fDGOlmgF1NE",
"category": "Education",
"title": "Jingle Bells Christmas Song",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/fDGOlmgF1NE/hqdefault.jpg"
},
{
"id": "Y83fbhN6FBk",
"category": "Film",
"title": "Pussy Cat Pussy Cat where have you been? - Nursery Rhyme",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/Y83fbhN6FBk/hqdefault.jpg"
},
{
"id": "UuqNHZEIwEI",
"category": "Film",
"title": "Thank you for the world so sweet - Kids Song",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/UuqNHZEIwEI/hqdefault.jpg"
},
{
"id": "g0u1iWUmg8Q",
"category": "Film",
"title": "Ding dong bell - Nursery Rhyme",
"thumbnail": "http://www.earlykid.com/android/timthumb.php?w=600&h=330src=http://i1.ytimg.com/vi/g0u1iWUmg8Q/hqdefault.jpg"
}
]
}
Parsing
try {
JSONObject jb = new JSONObject(myjsonstring);
String totalitems = jb.getString("totalItems");
Log.i("......", "" + totalitems);
String totalpages = jb.getString("totalPages");
String itemsPerPage = jb.getString("itemsPerPage");
String currentPage = jb.getString("currentPage");
JSONArray jarr = jb.getJSONArray("items");
for (int i = 0; i < jarr.length(); i++) {
JSONObject jb1 = jarr.getJSONObject(i);
String id = jb1.getString("id");
String categoy = jb1.getString("category");
String title = jb1.getString("title");
String pic = jb1.getString("thumbnail");
Log.i("........", id);
}
} catch (Exception e) {
}
Try this :
JsonObject oJsonObject = new JSONObject(myjsonstring);
if(oJsonObject.has("items"))
{
oJsonArray = oJsonObject.getJSONArray("items");
for(int i = 0; i < oJsonArray.length(); i++)
{
String id = oJsonArray.getJSONObject(i).getString("id");
String title = oJsonArray.getJSONObject(i).getString("title");
String category = oJsonArray.getJSONObject(i).getString("category");
String thumbnail = oJsonArray.getJSONObject(i).getString("thumbnail");
}
}
Actually in your JSON you have missed a } at the end of JOSN data so it is not a vlid JSON which you have posted over here
I want to parse json, but I didn't find how to parse array from this structure:
{
"0": {
"title": "\u0417\u041d: \u0415\u0432\u0440\u043e\u043f\u0435\u0439\u0446\u044b ",
"date": "2011-11-26 14:33:00"
},
"1": {
"title": "\u041a\u0430\u043a\u0430\u044f ",
"date": "2011-11-25 13:55:00"
},
"2": {
"title": "\u0423\u043a\u0440\u0430\u0438\u043d\u0430",
"date": "2011-11-25 11:15:00"
},
"3": {
"title": "\u0423\u0416\u0421\u041a ",
"date": "2011-11-24 15:45:00"
},
"time": 0.03944993019104
}
See, the problem is you don't actually have an array. You have a series of dictionaries, keyed by index. The only way to do this is to iterate over each numerical key, and add its value to a list.
Here's some pseudocode to help you get started:
yourArray = new Array(yourJSON.keys.length)
for key in yourJSON.keys:
yourArray.put(yourJSON[key], int(key))
You'll need to make a new array object whose length is equal to the number of keys. Then, put each of the values for each key at index key.
Try parsing these as JSONObject and then accessing it's values keys provided.
To be more specific:
try {
JSONObject foo = new JSONObject(youJsonString);
foo.get(name)
} catch (JSONException e) {
//handle exceptions
}