how to get json data with android
my json like this:
[
{
"meta": {
"next_id": 30
}
},
{
"data": [
{
"category_id": "2",
"name": "Anniversary Facebook Status",
"count": "53"
},
{
"category_id": "4",
"name": "April Fool Status",
"count": "16"
},
{
"category_id": "79",
"name": "Wise Facebook Status",
"count": "90"
}
]
}
]
My code
JSONObject jsonobject = JSONfunctions.getJSONfromURL("URL");
JSONArray jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Log.i("ID", jsonobject.getString("category_id"));
Log.i("Name", jsonobject.getString("name"));
}
i want get "data" array.
please help if anybody know:
Thank You.
Try this.
JSONArray total_array = new JSONArray(string json);
JSONArray data_array = total_array.getJSONObject(1).getJSONArray("data");
for(int i = 0; i < data_array.length(); i++){
JSONObject json_data = data_array.getJSONObject(i);
String category_id = json_data.getString("category_id");
}
Probably this may help you :
JSONArray jsonArray;
try {
jsonArray = new JSONArray("YOUR JSON DATA");
Log.v("MAIN ACTIVITY", "JSON OBJECT :"+jsonArray.getJSONObject(0).getJSONObject("meta").getString("next_id"));
JSONArray dArray = jsonArray.getJSONObject(1).getJSONArray("data");
for(int i=0;i<dArray.length();i++)
{
JSONObject jsonObject = dArray.getJSONObject(i);
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("category_id"));
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("name"));
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("count"));
}
}
catch (JSONException e1) {
e1.printStackTrace();
}
to get json array
JSONArray jsonArray = new JSONArray(jsonStr);
JSONArray dataArray= jsonArray.getJSONObject(1).getJSONArray("data");
for(int i=0;i<dataArray.length();i++){
JSONObject json_data = dataArray.getJSONObject(i);
String category_id = json_data.getString("category_id");
String name = json_data.getString("name");
String count = json_data.getString("count");
}
For more info see Android JSON Parsing Tutorial
Look into the Gson library by Google. It's easier than parsing the fields manually with the included JSON lab.
Related
I have string json like this :
{
"listResult": {
"items": [
{
"id": "629047db-66d9-4986-ba3f-c75554198138",
"thumbnail": "http://maya-wdv-01.r.worldssl.net/39aa32db-6f50-4da1-8fd5-a5b001135b98/629047db-66d9-4986-ba3f-c75554198138/8cb69c17-0fdb-454c-bfb5-a5b9001a9d59.jpg"
},
{
"id": "fa872dc8-d2b3-4815-92ef-d90e903bc3d8",
"thumbnail": "http://maya-wdv-01.r.worldssl.net/39aa32db-6f50-4da1-8fd5-a5b001135b98/fa872dc8-d2b3-4815-92ef-d90e903bc3d8/c510c24f-5bfd-4a64-8851-a5b90017a38d.jpg"
}
],
"totalItems": 34,
"pageSize": 5,
"pageNumber": 1,
"totalPages": 7,
"searchTerm": null
}
}
I Try Parsing with code :
try {
JSONObject json = new JSONObject(response);
String listResult = json.getString(Variabel.listResult);
JSONArray items_obj = json.getJSONArray(Variabel.items);
int jumlah_list_data = items_obj.length();
if (jumlah_list_data > 0) {
for (int i = 0; i < jumlah_list_data; i++) {
JSONObject obj = items_obj.getJSONObject(i);
String id = obj.getString(Variabel.id);
String thumbnail = obj.getString(Variabel.thumbnail);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
But I getting Error :
org.json.JSONException: No value for items
So how to solve it ? sorry for my English
Instead of:
JSONObject json = new JSONObject(response);
String listResult = json.getString(Variabel.listResult);
JSONArray items_obj = json.getJSONArray(Variabel.items);
you should have:
JSONObject json = new JSONObject(response);
JSONObject listResult = json.getJSONObject(Variabel.listResult);
JSONArray items_obj = listResult.getJSONArray(Variabel.items);
Accordingly to the json you posted, items is direct child of listResult, so you have to use the JSONObject with key listResult to retrieve it. Change
JSONArray items_obj = json.getJSONArray(Variabel.items);
with
JSONArray items_obj = listResult.getJSONArray(Variabel.items);
Initial JSON
{
"data": [
{
"request": [
[
{
"name": "John""email": "John#gmail.com"
}
],
[
"name": "William""email": "William#gmail.com"
]
]
}
]
}
Corrected JSON as it was not passing through JSONLint
{
"data": [
{
"request": [
{
"name": "John",
"email": "John#gmail.com"
},
{
"name": "William",
"email": "William#gmail.com"
}
]
}
]
}
How to extract an array of JSON inside JSON array.I got error message as
json array cannot be converted into JSON Object.
Following code will help to parse your Json:
try {
JSONObject jsonData = new JSONObject(jsonObject);
JSONArray dataJsonArray = jsonData.getJSONArray("data");
JSONArray jsonRequest = dataJsonArray.getJSONObject(0)
.getJSONArray("request");
JSONArray mRequestChildJson = jsonRequest.getJSONArray(0);
JSONObject innerJson = mRequestChildJson.getJSONObject(0);
LogHandler.e("TAG",
"mRequestChildJson" + innerJson.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
don't forgot to add check of has Key and size of array.
may be you're on right track but casting it in object
JSONArray JArray = jObject.getJSONArray("data");
try {
String str = result.toString();
System.out.println("prov-request -->"+str);
JSONObject json=new JSONObject(str);
JSONArray jsonarray = json.getJSONArray("data");
for(int i=0;i<jsonarray.length();i++){
JSONObject jsonnew=jsonarray.getJSONObject(i);
JSONArray jsonarrayROW = jsonnew.getJSONArray("request");
for(int j=0;j<jsonarrayROW.length();j++){
JSONObject jsonObject=jsonarrayROW.getJSONObject(j);
date.add(jsonObject.getString("name"));
from.add(jsonObject.getString("email"));
}
} catch (JSONException e) {
e.printStackTrace();
}
Before I parse json json array, but this json object. Its It drove me to a standstill. How i can parse json like this:
{ "test1": {
"1": {
"action": "0",
"type": "1",
"id": "1",
},
"2": {
"action": "0",
"type": "1",
"id": "2",
}
},
"test2": {
"1": {
"id": "1",
"name": "one"
},
"2": {
"id": "2",
"name": "two"
},
"5": {
"id": "5",
"name": "three"
}
}}
When you don't have a fixed set of keys, that you know upfront, the only way to parse it is to use keys(). It returns an Iterator with the keys contained in the JSONObject. In your case you could have
JSONObject jsonObject = new JSONObject(...);
Iterator<String> iterator = jsonObject.keys();
while(iterator.hasNext()) {
String currentKey = iterator.next();
JSONObject obj = jsonObject.optJSONObject(key);
if (obj != null) {
Iterator<String> iterator2 = obj.keys();
}
}
iterator will return test1 and test2, while iterator2 will return 1 and 2, for test1 and 1 ,2 , 5 for test2
You can create a JSONObject From string as bellow
JSONObject jsonObject = new JSONObject(YOUR_JSON_STRING);
and to parse the jsonObject
JSONObject test1Json = jsonObject.getJSONObject("test1");
JSONObject oneTest1Json = test1Json.getJSONObject("1");
to get String values
String action = oneTest1Json.getString("action");
String type = oneTest1Json.getString("type");
String id = oneTest1Json.getString("id");
Log.d("Json parse","action -"+action+" type -"+type+" id -"+id);
if need them as JSONArray the you can try
public JSONArray getJsonArray (JSONObject jsonObject){
JSONArray nameJsonArray = jsonObject.names();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < nameJsonArray.length(); i++) {
try {
String key = nameJsonArray.getString(i);
jsonArray.put(jsonObject.getString(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonArray;
}
in case keys like test1,test2,test3...
JSONObject jsonObject = new JSONObject("{'test1':'Kasun', 'test2':'columbo','test3': '29'}");
JSONArray jsonArray = new JSONArray();
for (int i = 1; i <= jsonObject.names().length(); i++) {
try{
jsonArray.put(jsonObject.getString("test" + i));
}catch (JSONException e){
e.printStackTrace();
}
you can get your JSONArray this way.
I am new in Json parsing. I am receiving Json data from my url which is given below:
[
[
{
"message": "hdjcjcjjckckckckvkckckck",
"timetoken": 14151866297757284
},
{
"message": "ufjfjfjcjfjchfjdhwjroritkgjcj",
"timetoken": 14151869212145692
},
{
"message": "udjfjfudjcyshdhsnfkfkvkf",
"timetoken": 14151869317015766
},
{
"message": "lvkifjsywjfwhsjvjdjfudjgidufkg",
"timetoken": 14151869404695072
},
{
"message": "ifjfydncydhsxhshfjdjejtlfudj",
"timetoken": 14151869494732788
},
{
"message": "22637485969473849506&#&#*%-&+",
"timetoken": 14151869589393336
},
{
"message": "jcjfjdueywjfusufig",
"timetoken": 14151869671994892
},
{
"message": "ofkdiriflfkkkfkdiidk",
"timetoken": 14151869775170644
},
{
"message": "testing",
"timetoken": 14151869895179728
},
{
"message": 1234567,
"timetoken": 14151869986900556
},
{
"message": 9877653,
"timetoken": 14151870106439620
},
{
"message": "zxcvbnmmkljgdaqerip",
"timetoken": 14151870236042386
}
],
14151866297757284,
14151870236042386
]
I am using this to break JsonArray data into different index like I want to display message and timetoken in separate lines in my activity, like this:
message: hdjcjcjjckckckckvkckckck
time token: 14151866297757284
message: 22637485969473849506&#&#*%-&+
time token: 14151869212145693
What should I have to do in the following line of codes:
JSONArray jsonObj = new JSONArray(message.toString()); //message.toString() is the Json Response data
for (int i = 0; i < jsonObj.length(); i++) {
}
I want to display it in my Textview as described above.
Try this one (not tested):
JSONArray jsonObj = new JSONArray(message.toString()); //message.toString() is the Json Response data
JSONArray array = jsonObj.getJSONArray(0);
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.getJSONObject(i);
String mesage = item.getJsonString("message");
String timespan = item.getJsonString("timespan");
}
you should put json parsing into try-catch block:
try{
JSONArray res = new JSONArray(response.toString());
JSONArray jsonArray = res.getJSONArray(0);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String message = object.getString("message");
String token = object.getString("timetoken");
}
}catch(Exception e){
e.printStackTrace();
}
you have a double array, so you have two JSONArray. Actually, JSONArray usually marked by [] and JSONObject marked by {}.
In other words {}=JSONObject and []=JSONArray.
I have solved the problem. Actually the Json string has double Json Array and then Json Object.
I was doing a mistake to send an object of Json Array to Json Object. Now I have make an object of Json Array1 then send it to Json Array2 and then send the object of Json Array2 in Json Object.
The code is given below:
try {
JSONArray jsonObj = new JSONArray(message.toString());
JSONArray jArray = new JSONArray(jsonObj.get(0).toString());
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
String messageString=c.getString("message");
String timeString=c.getString("timetoken");
String abc = timeString;
}
}
I have the following response:
{
"data": [
{
"name": "This is a test 123",
"start_time": "2013-12-02T18:00:00+0530",
"end_time": "2013-12-02T20:00:00+0530",
"location": "Mount Lavinia",
"id": "525447507473743",
"rsvp_status": "attending"
},
{
"name": "This is a test event",
"start_time": "2013-12-02T18:00:00+0530",
"end_time": "2013-12-02T20:00:00+0530",
"location": "Mount Lavinia",
"id": "560383743988530",
"rsvp_status": "attending"
},
}
],
I have the following code to access the response:
JSONObject jsonObj = new JSONObject(result);
JSONArray jsonArr = jsonObj.getJSONArray("data");
for(int i = 0; i < jsonArr .length(); i++)
{
JSONObject jsonArrObj = jsonArr.getJSONObject(i);
String id = jsonArrObj.getString("id");
String username = jsonArrObj.getString("name");
Log.w("event names are", username.toString());
}
I get the following error:
json.JSON.typeMismatch(JSON.java:100)
I searched for a solution but could not get any.
this is the full parsing of your JSON string you are getting form graph.facebook.com API :
ArrayList<HashMap<String,String>> arrahaspmap=new
ArrayList<HashMap<String,String>>();
HashMap<String,String> hashmaptemp;
JSONObject jsonObj;
try {
jsonObj = new JSONObject(sdskd);
JSONArray jsonArr = jsonObj.getJSONArray("data");
System.out.println("jsonArr jsonArr jsonArr jsonArr :: "+jsonArr.length());
for(int i = 0; i < jsonArr .length(); i++)
{
JSONObject jsonArrObj = jsonArr.getJSONObject(i);
hashmaptemp=new HashMap<String, String>();
hashmaptemp.put("id", jsonArrObj.getString("id"));
hashmaptemp.put("end_time", jsonArrObj.getString("end_time"));
hashmaptemp.put("location", jsonArrObj.getString("location"));
hashmaptemp.put("name", jsonArrObj.getString("name"));
hashmaptemp.put("rsvp_status", jsonArrObj.getString("rsvp_status"));
arrahaspmap.add(hashmaptemp);
}
JSONObject jsonobjpaging= jsonObj.getJSONObject("paging");
String strprevious=jsonobjpaging.getString("previous");
String strnext=jsonobjpaging.getString("next");
System.out.println("jsonobjpaging previous :: "+strprevious);
System.out.println("strnext next :: "+strnext);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}