GSON parse Array in array in array - android

I face this problem, from the API I'm getting an extens JSON with one object called coordinates, this object is an array that includes an array o array.
To be more clear look at this example:
"coordinates": [
[
[
-0.087118,
51.508823
]
Or in this pic you can see it better
So now I'm trying to parse it using GSON and I'm not being success. Can anyone give me an idea how to create the class for this?
Thank you

public class Coordinate extends ArrayList<Arraylist<Arraylist<float>>>{
}

I can give you an example, where you wouldn't use Gson, but you'll get the idea.
Lets say that you put your JSON responce in a String called json
String json = ""coordinates": [[[-0.087118,51.508823]";
JSONArray firstArray = new JSONArray(json);
for (int i =0; i < firstArray.length(); i++){
JSONArray secondArray = firstArray.getJSONArray(i);
for (int j =0; i < secondArray.length(); j++){
JSONArray thirdArray = secondArray.JSONArray(j);
for (int q =0; i < thirdArray.length(); q++){
int x = thirdArray.getInteger("0");
int y = thirdArray.getInteger("1");
}
}
}

Related

JSON parse a JsonObject without key

I'm trying to parse a JSON which contains:
"images": [
"https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg",
"https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg",
"http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg"
]
The problem is that I don't know how to get the string of elements inside the "images" node
My code currently looks like:
if(c.has("images") && !c.isNull("images")){
JSONArray imagenes = jsonObj.getJSONArray("images");
for (int j = 0; j < imagenes.length(); j++) {
JSONObject m = imagenes.getJSONObject(i);
}
}
How can I get each string in the array without using a "key"?
I don't know what to do with "M" next.
You want a string, not an object. Strings don't have nested keys.
Use getString(i) method instead of getJSONObject(i)
Your images array contains with string values not with json object. So you need to get string instead of jsonObject.
if(c.has("images") && !c.isNull("images")){
JSONArray imagenes = jsonObj.getJSONArray("images");
for (int j = 0; j < imagenes.length(); j++) {
String imgURL = imagenes.optString(i);
System.out.println(imgURL);
}
}
Output:
https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg
https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg
http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg

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
}

JSON data list dynamic

I am working in Android just for a heads up. I was able to retrieve JSON data but now I have come to the point where there are multiples. Example:
"workers":[
{
"id":3357,
"username":"Unreliable.worker",
"password":"x",
"monitor":0,
"count_all":41,
"count_all_archive":0,
"hashrate":477,
"difficulty":106.56
},
{
"id":4061,
"username":"Unreliable.worker2",
"password":"x",
"monitor":0,
"count_all":0,
"count_all_archive":null,
"hashrate":0,
"difficulty":0
}
would I have to do a for loop or is there another way to do it?
This is the code that I am using now to get them:
JSONObject workersJSONObject = personalJSONObject.getJSONObject("workers");
but I don't know how I would get for each and separate them. I'll get them by using:
id= workersJSONObject.getDouble("id");
[] in JSON refers to a JSON array. You could use something like:
JSONArray workersJSONArray = personalJSONObject.getJSONArray("workers");
Then loop through each item as (basically get each object inside array):
for (int i = 0; i < workersJSONArray.length(); i++) {
JSONObject workersJSONObject = workersJSONArray.getJSONObject(i);
// parse object just like before
}
On JSON, {} defines an object, [] defines an array instead. if you want details. see this and JSONArray
sample:
JSONArray workersJSOnArray = new JSONArray(stringData);
or
JSONArray workersJSOnArray = personalJSONObject.getJSONArray("workers");
/* THen you can loop from each data you want */
int len = workersJSOnArray.length();
for (int i = 0; i < len; i++)
{
JSONObject item = workersJSOnArray.optJSONObject(i);
int id = item.getInt ("id");
.....
}

Traverse array IN JSONArray

I'm making an app for android which makes a call to a webservice that returns a json with ARRAY as a parameter, I can go through all the settings and save them easily.
The problem is when I get the array that I returned within the JSON object.
Example of JSON:
[{"codigoArticulo":"0001","nombreArticulo":"CHULETAS DE CORDERO","factorVentasDefecto":"KG","precio":21.95,"factoresDeVenta":["KG","UN"]},{"codigoArticulo":"0007","nombreArticulo":"FALDETA DE CORDERO","factorVentasDefecto":"KG","precio":11.95,"factoresDeVenta":["KG","FL"]}]
I can save "codigoArticulo", "nombreArticulo", "factorVentasDefecto" and "precio easily, BUT i don't know how i can save "factoresDeVenta".
i have this code:
JSONArray resparray = new JSONArray(JSONdevuelto);
for (int i = 0; i < resparray.length(); i++) {
JSONObject respJSON = resparray.getJSONObject(i);
int IDArticulo = respJSON.getInt("codigoArticulo");
String NombreArticulo = respJSON.getString("nombreArticulo");
String FactordeVenta = respJSON.getString("factorVentasDefecto");
int PrecioArticulo = respJSON.getInt("precio");
}
How i can save in one array the variables on "factoresDeVenta"?
I try
String[] Factores = respJSON.getJSONArray("factoresDeVenta");
but no works because are incompatible types.
I need the array to make later a Spinner
Thank you.
factoresDeVenta is an JSONArray inside JSONObject so you will need to use getJSONArray or optJSONArray and use loop for getting values from JSONArray:
JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");
for (int i = 0; i < jArray.length(); i++) {
String str_value=jArray.optString(i); //<< jget value from jArray
}
JSONArray jArray = respJSON.getJSONArray("factoresDeVenta");
It would give you the array of data stored in factoresDeVenta, which you can again traverse to get the individual elements from that array.
You can store in ArrayList<String>
ArrayList<String> arrStr = new ArrayList<String>();
JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");
for (int i = 0; i < jArray.length(); i++) {
arrStr.add(jArray.getString(i));
}
And then you can convert arraylist to string array
String[] Factores = arrStr.toArray(new String[arrStr.size()]);

How to fetch Data From JSON

I have following data from JSON
{
"MenuName": "starter dish",
"SubMenu": [
"pizza dish1",
"pizza dish2"
],
"Price": [
"100",
"110"
]
},
From here i can easily fetch data from "Menuname" key as starter dish but when I fetch data from "Submenu" I get whole string as ["pizza dish1", "pizza dish2"].
Please suggest me way to differentiate both pizza dish1 and pizza dish2
Submenu and Price is not String it is JSONArray so you will need to use for-loop to get all values from Submenu JSONArray as:
JSONArray jsonsubmenu=yourjsonobject.getJSONArray("Submenu");
for(int i=0;i < jsonsubmenu.length();i++){
// get all values from jsonsubmenu JSONArray..
String str_value=jsonsubmenu.optString(i);
....
}
try this
for (int i = 0; i < jsonArray.length(); i++) {
jsonArray.getJSONObject(i).getInt("SubMenu");
}
you can use this link for creating POGO class for your response. It will automatically generate class for your response.
Use google GSON library to parse your response.
or you can simply create a JSON Array or Objects to parse it. in your case it is JSON Object or JSON arrays.
Hi There in that senarerio you have to use
JSONArray _submenu = object.getJSONArray("SubMenu");
for (int i = 0; i < _submenu.length(); i++) {
String text = _submenu.getString(i);
}
JSONArray _price = object.getJSONArray("Price");
for (int i = 0; i < _price.length(); i++) {
String text = _price.getString(i);
}
You can retrieve array values and store it in string[] and use them as per your need. i.e., as follows..
try {
JSONObject jObject = new JSONObject(jsonString);
JSONArray jSubmenu = jObject.getJSONArray("SubMenu");
subMenu = new String[jSubmenu.length()];
for (int i = 0; i < subMenu.length; i++) {
subMenu[i] = jSubmenu.getString(i);
}
JSONArray jPrice = jObject.getJSONArray("Price");
price = new String[jPrice.length()];
for (int i = 0; i < price.length; i++) {
price[i] = jPrice.getString(i);
}
} catch (Exception e) {
// TODO: handle exception
}
Just to throw in a quickie - read up on, and use GSON.
For simple small jobs I find it is the best. Not the fastest running for complex, or long structures, but really quick on the dev side.
Here's the link: google-gson

Categories

Resources