I have an array jArray in the format :
{"users":[
{
"user_id":6,
"user_name":"Ted Vanderploeg",
"email":"test5#test.com",
"additional_info":[["HP","Chief Sales Officer","","",1]]
},
{
"user_id":59,
"user_name":"Lindsay White",
"email":"test12260#test.com",
"additional_info":[["Microsoft","Global Head","","",1]]
}
]
}
Now I need to get the value "Microsoft" from additional_info array. This is what I'm trying :
for(int i=0;i<jArray.length();i++) {
JSONObject jsonObject = new JSONObject(jArray.getString(i));
String workInfo = jsonObject.getString("additional_info");
Log.i("MyActivity", "got work obj as " + workInfo.toString());
}
Now I get workInfo as [["Microsoft","Global Head","","",1]]. Stuck on how to proceed next, to get the value Microsoft.
it looks like a JSONArray inside a JSONArray
for(int i=0;i<jArray.length();i++) {
JSONObject jsonObject = new JSONObject(jArray.getString(i));
JSONArray workinfo = jsonObject.optJSONArray("additional_info");
if (workinfo != null) {
for(int j=0;j<workinfo.length();j++) {
JSONArray values = workinfo.optJSONArray(j);
for(int z=0;z<values.length();z++) {
Log.i("MyActivity", "got work obj as " + values.optString(z));
}
}
}
}
Related
How to parse JSON values in this format? I want to get the details of the data element but inside data there are 'dates' and inside dates there is array containing two more elements. I want to get all the dates first inside data and then within these dates I want all the information within these dates. How can I achieve this? Please Help. I tried with below code but it hasn't worked
try {
JSONObject jsonObject = new JSONObject("data");
JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));
JSONArray session;
for (int i = 0; i < jsonArray.length() - 1; i++) {
jsonObject = jsonArray.getJSONObject(i);
session= jsonObject.getJSONArray("session");
Log.d("MyLog", session + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
Following is the format
{
"status": 1,
"status_code": 200,
"data": {
"2018-02-11": [
{
"session": "01:00 AM",
"place": true
},
{
"session": "02:00 AM",
"place": true
}
],
"2018-02-12": [
{
"session": "01:00 AM",
"place": true
},
{
"session": "02:00 AM",
"place": true
}
]
}
}
You just need to pass the response string to the method. You can try this:
private void jsonParsing(String jsonString) {
// String jsonString = "{ \"status\": 1, \"status_code\": 200, \"data\": { \"2018-02-11\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ], \"2018-02-12\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ] } }";
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject dataObj = jsonObject.getJSONObject("data");
Iterator<String> iter = dataObj.keys();
Log.e(TAG, "jsonParsing: "+iter );
while (iter.hasNext()) {
String key = iter.next();
JSONArray datesArray = dataObj.getJSONArray(key);
ArrayList<String> sessions = new ArrayList<String>();
for (int i = 0; i < datesArray.length(); i++) {
JSONObject datesObject = datesArray.getJSONObject(i);
sessions.add(datesObject.getString("session"));
}
Log.d("MyLog", sessions + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
(1) get JSONObject of Main json
JSONObject objMain = new JSONObject("your json string");
(2)get JSONObject of "data" from main json
JSONObject jsonData = objMain.getJSONObject("data")
(3) get all keys (dates) from object "data"
Iterator<String> iter = jsonData.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONArray arrayDate = objData.getJSONArray(key)
for (i = 0; i < arrayDate.length(); i++) {
JSONObject objDate = arrayDate.getJSONObject(i)
Log.d("#session :", "" + objDate.getString("session"))
Log.d("#place :", "" + objDate.getBoolean("place"))
}
} catch (JSONException e) {
// Something went wrong!
}
}
try this one code
IN THIS CODE jsonMstObject IS TEMP OBJECT YOU HAVE TO USE YOUR API RESPONSE JSONobject INSTEAD OF jsonMstObject
try {
JSONObject jsonMstObject = new JSONObject("{"status":1,"status_code":200,"data":{"2018-02-11":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}],"2018-02-12":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}]}}");
JSONObject jsonObject = jsonMstObject.getJSONObject("data");
JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));
ArrayList<String> arrSession = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
arrSession.add(jsonObject.getString("session"));
}
Log.d("MyLog", arrSession + "");
} catch (JSONException e) {
e.printStackTrace();
}
in this code arrSession is your session string array
Ex. you passed cuurentdate = "2018-02-11" then you recived result like
[01:00 AM, 02:00 AM]
Note: this code is worked based on your cuurentdate param. This is code for get static date array from data and create String Array.
I am new to android, As I am trying to parse the Google's spreadsheet data and it is somewhat complex to me, I have to fetch $t of gsx$tag and $t of gsx$datetime,
This is the link from which we can get the JSON data
https://spreadsheets.google.com/feeds/list/1_AR0zX6Jv0NI_R1HBULbPEIUuJ2mqVbLoHVvLqOwu1I/1/public/values?alt=json
Data look like this
{
"version":"1.0",
"encoding":"UTF-8",
"feed":{
"xmlns":"http://www.w3.org/2005/Atom",
"xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/",
"xmlns$gsx":"http://schemas.google.com/spreadsheets/2006/extended",
"id":{ },
"updated":{ },
"category":[ ],
"title":{ },
"link":[ ],
"author":[ ],
"openSearch$totalResults":{ },
"openSearch$startIndex":{ },
"entry":[
{
"id":{ },
"updated":{ },
"category":[ ],
"title":{ },
"content":{ },
"link":[ ],
"gsx$id":{
"$t":"1"
},
"gsx$datetime":{
"$t":"3/28/2017"
},
"gsx$tag":{
"$t":"21"
}
},
{ Data here },
{ Data here }
]
}
}
and below is the code I am trying
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject mainObj = new JSONObject(jsonStr);
Log.e(TAG,"JSon Data"+mainObj);
if (mainObj != null) {
Log.e(TAG,"JSon before feed"+mainObj.toString());
JSONArray a = mainObj.getJSONArray("feed");
Log.e(TAG,"JSon after feed"+a.toString());
JSONObject entru= new JSONObject((Map) a);
JSONArray list = entru.getJSONArray("entry");
// JSONArray entryarray = list.getJSONArray("entry");
if (list != null) {
for (int i = 0; i < list.length(); i++) {
JSONObject elem = list.getJSONObject(i);
if (elem != null) {
JSONArray prods = elem.getJSONArray("gsx$tag");
if (prods != null) {
for (int j = 0; j < prods.length(); j++) {
JSONObject innerElem = prods.getJSONObject(j);
if (innerElem != null) {
String sku = innerElem.getString("$t");
Log.e(TAG, "$t" + sku);
HashMap<String, String> contact = new HashMap<>();
contact.put("$t Datetime", sku);
// adding contact to contact list
contactList.add(contact);
}
}
}
}
}
}
}
Help Me in this regards, Thank you in advance
JSONArray a = mainObj.getJSONArray("feed");
Log.e(TAG,"JSon after feed"+a.toString());
JSONObject entru= new JSONObject((Map) a);
replace this 3 lines with the below one
JSONObject entru= mainObj.getJSONObject("feed");
also replace this line
JSONArray prods = elem.getJSONArray("gsx$tag");
with this one
JSONObject prods = elem.getJSONObject("gsx$tag");
I think this is the correct code.
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject mainObj = new JSONObject(jsonStr);
Log.e(TAG,"JSon Data"+mainObj);
if (mainObj != null) {
Log.e(TAG,"JSon before feed"+mainObj.toString());
JSONObject entru= mainObj.getJSONObject("feed");
JSONArray list = entru.getJSONArray("entry");
//JSONArray entryarray = list.getJSONArray("entry");
if (list != null) {
for (int i = 0; i < list.length(); i++) {
JSONObject elem = list.getJSONObject(i);
if (elem != null) {
JSONObject prods = elem.getJSONObject("gsx$tag");
if (prods != null) {
for (int j = 0; j < prods.length(); j++) {
String sku = prods.getString("$t");
Log.e(TAG, "$t" + sku);
HashMap<String, String> contact = new HashMap<>();
contact.put("$t Datetime", sku);
// adding contact to contact list
contactList.add(contact);
}
}
}
}
}
}
Please try this. If this doesn't work please feel free to ask. will help you
You can try using Gson or Jackson
my tryouts but its give me error
My JSON:
{
"status":true,
"message":"Front About Found",
"data":{
"FrontAbout":{
"ABOUT_ID":"1",
"CONTENT":"and the arts. ",
"SCHOOL_NAME":"The New School",
"ADDRESS":" Western Corridor",
"LOGO":"14643339731.png"
}
}
}
My Sample code:
{
JSONArray jsonarray = jObj.getJSONArray("data");
for (int z = 0; z < jsonarray.length(); z++) {
JSONObject jobj = jsonarray.getJSONObject(z);
JSONObject jobj_FrontAbout = jobj.getJSONObject("FrontAbout");
school = jobj_FrontAbout.getString("SCHOOL_NAME");
add = jobj_FrontAbout.getString("ADDRESS");
phone = jobj_FrontAbout.getString("CONTACT_NO");
email = jobj_FrontAbout.getString("EMAIL");
cont = jobj_FrontAbout.getString("CONTENT");
image = jobj_FrontAbout.getString("LOGO");
setData();
loadingView.dismiss();
}
loadingView.dismiss();
if (loadingView != null && loadingView.isShowing()) {
loadingView.dismiss();
}
}
its give me error " JSONObject can not to be converted to JSONArray "
i want to simple set in textview not in array
please give me solution
Here are you are parsing for "data" as a JSOnArray Although "data" is a JSONObJect .
JSON nodes will start with a square bracket or with a curly bracket. The difference between [ and { is, the square bracket ([) represents starting of an JSONArray node whereas curly bracket ({) represents JSONObject.
{
JSONObject dataJsonObject = jObj.getJSONObject("data");
for (int z = 0; z < dataJsonObject.length(); z++) {
JSONObject jobj = jsonarray.getJSONObject(z);
JSONObject jobj_FrontAbout = jobj.getJSONObject("FrontAbout");
school = jobj_FrontAbout.getString("SCHOOL_NAME");
add = jobj_FrontAbout.getString("ADDRESS");
phone = jobj_FrontAbout.getString("CONTACT_NO");
email = jobj_FrontAbout.getString("EMAIL");
cont = jobj_FrontAbout.getString("CONTENT");
image = jobj_FrontAbout.getString("LOGO");
setData();
loadingView.dismiss();
}
loadingView.dismiss();
if (loadingView != null && loadingView.isShowing()) {
loadingView.dismiss();
}
}
HopeFully You will get rid of your error . CHEERS
you can parse like this.
JSONObject jobj = jsonarray.getJSONObject(z);
status=jobj.getBoolean("status");
message=jobj.getString("message");
JSONObject dataObject = jobj.getJSONObject("data");
JSONObject frontAboutObject = dataObject.getJSONObject("FrontAbout");
school = frontAboutObject.getString("SCHOOL_NAME");
add = frontAboutObject.getString("ADDRESS");
phone = frontAboutObject.getString("CONTACT_NO");
email = frontAboutObject.getString("EMAIL");
cont = frontAboutObject.getString("CONTENT");
image = frontAboutObject.getString("LOGO");
Try this:
JSONObject jsonObj= jObj.getJSONObject("your json string");
JSONObject jsonInnerObj= jsonObj.getJSONObject("data");
I need to get Particular Object values ( A, B, C, D) and related key values (#"name" ). After getting (A, B, C, D ) object values I need to list out into list view Android. Here below I have posted my sample code and response. Please help me.
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray("response");
Log.d("Response: ", "> " + contacts);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
My JSON Response :
{"response" : [ {
"A" : [ {
"name" : "tango"
},
{
"name" : "ping"
}
],
"B" : [ {
"name" : "tango"
},
{
"name" : "ping"
}
]
} ]}
Use JSONObject keys() to get the key and then iterate each key to get to the dynamic value.
You can get dynamic keys like this
JSONObject responseDataObj = new JSONObject(responseData);
JSONArray responseArray = responseDataObj.getJSONArray("response");
for (int i = 0; i < responseArray.length(); i++) {
nodes = new ArrayList<ArrayList<String>>();//nodes ArrayList<ArrayList<String>> declared globally
nodeSize = new ArrayList<Integer>();//nodeSize ArrayList<Integer> declared globally
JSONObject obj = responseArray.getJSONObject(i);
Iterator keys = obj.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
//store key in an arraylist which is A,B,...
// get the value of the dynamic key
JSONArray currentDynamicValue = obj.getJSONArray(currentDynamicKey);
int jsonrraySize = currentDynamicValue.length();
int sizeInArrayList = jsonrraySize + 1;
nodeSize.add(sizeInArrayList);
if(jsonrraySize > 0) {
for (int ii = 0; ii < jsonrraySize; ii++) {
nameList = new ArrayList<String>();//nameList ArrayList<String> declared globally
if(ii == 0) {
JSONObject nameObj = currentDynamicValue.getJSONObject(ii);
String name = nameObj.getString("name");
System.out.print("Name = " + name);
//store name in an arraylist
nameList.add(name);
}
}
}
nodes.add(nameList);
}
}
You can use following method to parse your response and handle output as per requirement.
private void parseJson(String res) {
try {
JSONObject mainObject = new JSONObject(res);
JSONArray response = mainObject.getJSONArray("response");
for (int i = 0; i < response.length(); i++) {
JSONObject obj = response.getJSONObject(i);
JSONArray A = obj.getJSONArray("A");
for (int j = 0; j < A.length(); j++) {
JSONObject objA = A.getJSONObject(j);
String name = objA.getString("name");
// use or store name here
}
JSONArray B = obj.getJSONArray("B");
for (int k = 0; k < B.length(); k++) {
JSONObject objB = B.getJSONObject(k);
String name = objB.getString("name");
// use or store name here
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This may work for you :
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray contacts;
contacts = jsonObj.getJSONArray("response");
Log.d("Response: ", "> " + contacts);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
JSONArray jsonArrayA=new JSONArray();
jsonArrayA=c.getJSONArray("A");
for(int j=0;j<jsonArrayA.length();j++){
String name=jsonArrayA.getJSONObject(j).getString("name");
Log.e("Name","name of jsonarray A "+i+" "+name);
}
JSONArray jsonArrayB=c.getJSONArray("B");
for(int k=0;k<jsonArrayB.length();k++){
String name=jsonArrayB.getJSONObject(k).getString("name");
Log.e("Name","name of jsonarray B "+i+" "+name);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
I've been experiencing some problems for quite some time trying to load a string that contains an array into a JSONArray.
i get the following string from a web-service which contains the movies array:
{"total":3,"movies":[],"links":{......}"}
i'm looking to convert this array into a JASONArray and show it using a list view.
i'm using the following code and it not working....
protected void onPostExecute(String result) {
Log.d(TAG, "result: " + result);
if (result==null || result.length()==0){
// no result:
return;
}
//clear the list
moviesList.clear();
try {
//turn the result into a JSON object
Log.d(TAG, "create responseObject: ");
JSONObject responseObject = new JSONObject(result);
Log.d(TAG, responseObject.toString());
// get the JSON array named "movies"
JSONArray resultsArray = responseObject.getJSONArray("movies");
Log.d(TAG, "JSONArray lenght: " + resultsArray.length());
// Iterate over the JSON array:
for (int i = 0; i < resultsArray.length(); i++) {
// the JSON object in position i
JSONObject movieObject = resultsArray.getJSONObject(i);
Log.d(TAG, "movieObject (from array) : " + movieObject.toString());
// get the primitive values in the object
String title = movieObject.getString("title");
String details = movieObject.getString("synopsis");
//put into the list:
Movie movie = new Movie(title, details, null,null);
//public Movie(String title, String details, String urlnet, String urldevice) {
moviesList.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
//refresh listView:
adapter.notifyDataSetChanged();
}
I was hoping a post here would help me solve the problem.
Change this:
JSONObject responseObject = new JSONObject(result);
to this:
JSONObject responseObject = (JSONObject) JSONValue.parse(result);
and then you can get your JSONArray.