This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
I currently have the following JSON structure:
{
"Apps": [
{
"column1": "sample string 1",
"column2": true
},
{
"column1": "sample string 1",
"column2": true
}
],
"param": true
}
How do I get the values of the column1 and column2? What I only know how to parse is a JSONObject within a JSONArray.
JSONObject jSONObject = new JSONObject(yourStrinig);
JSONArray jArray = jSONObject.getJSONArray("Apps");
for (int i = 0; i < jArray.length(); i++) {
JSONObject childrenObject = jArray.getJSONObject(i);
String column1 = childrenObject.getString("column1");
String column2 = childrenObject.getString("column2");
}
Something like this.
// Get a reference to the JSON object
JSONObject jSONObject = new JSONObject(stringJsonResponse);
// Getting the JSON array node
JSONArray jsonAray = jSONObject.getJSONArray("Apps");
// Looping through the json array
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject childrenObject = childrenArray.getJSONObject(i);
...
...
...
}
You can take a look at how I parsed JSON data when I received similar data https://github.com/gSrikar/AskReddit/blob/master/app/src/main/java/com/example/srikar/askreddit/MainActivity.java
with this you can loop through all elements
private void showJSON(String response){
String name="";
String phone_number="";
try {
JSONObject jsonObject =new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
for(int x=0;x<result.length();x++) {
JSONObject collegeData = result.getJSONObject(x);
name = collegeData.getString(config.KEY_NAME);
phone_number = collegeData.getString(config.KEY_PHONE_NUMBER);
//you can save your data in list here
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Related
I have json data as mentioned below.
{
"data":[
{
"Products":{
"id":"86",
"pname":"mi4",
"pcat":"9",
"subcat":"8",
"seccat":"0",
"oproduct":"1",
"pdetails":"Good phone",
"pprice":"10000",
"pdiscount":"10",
"qty":"1",
"qtytype":"GM",
"dcharge":"40",
"pimage":null,
"sname":"Easydeal",
"sid":"1100",
"size":"",
"pincode":""
}
}
]
}
I can identify array as getJSONArray("datas"). But I want to get pname and sname values.
Just reach the object
JSONObject resp=new JSONObject("response");
JSONArray data=resp.getJSONArray("data");
now if you want to get object at a particular index(say '0')
JSONObject objAt0=data.getJSONObject(0);
JSONObject products=objAt0.getJSONObject("products");
String pName=products.getString("pname");
you can similarly traverse the array
for(int i=0;i<data.lenght();i++){
JSONObject objAtI=data.getJSONObject(i);
JSONObject products=objAtI.getJSONObject("products");
String pName=products.getString("pname");
}
To get the to the key "Products" you should do:
JSONObject productsObject = YOUROBJECTNAME.getJSONArray("data").getJSONObject(0).getJSONObject("Products");
Then to get the values in productsObject you should do:
productsObject.getString("id");
productsObject.getString("pdetails");
And so on.
Try out the following code:
JSONObject object = new JSONObject(result);
JSONArray array = object.getJSONArray("data");
JSONObject object1 = array.getJSONObject(0);
JSONObject products = object1.getJSONObject("Products");
int id = object1.getInt("id");
String pname = object1.getString("pname");
This is how you get pname and sname:
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray;
try {
jsonArray = jsonObject.getJSONArray("data");
for(int counter = 0; counter <jsonArray.length(); counter++){
JSONObject jsonObject1 = jsonArray.getJSONObject(counter);
JSONObject products = jsonObject1.getJSONObject("Products");
String pname = products.getString("pname");
String sname = products.getString("sname");
}
} catch (JSONException e) {
e.printStackTrace();
}
PS: Poor JSON structure :)
This question already has answers here:
JSONArray cannot be converted to JSONObject error
(3 answers)
Closed 6 years ago.
I am having problems; I cannot read inner string from a jsonObject. It says JsonArray cannot be converted into JsonObject.
07-26 13:01:31.910 1798-1901/com.example.phuluso.aafs I/System.out: [{"AccommoAddress":{"AddressID":12,"City":"Johannesburg","InfoUrl":null,"Lattitude":"-26.181321","Longitude":"27.99158","PostalCode":2109,"Street":"22 Ararat Str","Town":"Westdene"},"AccommoDetails":null,"AccommoID":1,"AccommoImages":null,"AccommoName":"West Dunes Properties","AccommoType":"Flat","AccredStatus":"ACCREDITED","AddressId":12,"Capacity":9,"Distance":1,"EndDate":"2017-01-01","NearestCampus":"APK","OwnerId":0,"StartDate":"2016-01-01"}]
Here's my JsonArray. I am trying to read from AccommoAddress, but I get the error below:
[{"AccommoAddress":{"AddressID":12,"City":"Johannesburg","InfoUrl":null,"Lattitude":"-26.181321","Longitude":"27.99158","PostalCode":2109,"Street":"22 Ararat Str","Town":"Westdene"},"AccommoDetails":null,"AccommoID":1,"AccommoImages":null,"AccommoName":"West Dunes Properties","AccommoType":"Flat","AccredStatus":"ACCREDITED","AddressId":12,"Capacity":9,"Distance":1,"EndDate":"2017-01-01","NearestCampus":"APK","OwnerId":0,"StartDate":"2016-01-01"}]
Here's my code
#Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
List<AccommoNearAPK> data = new ArrayList<>();
progressDialog.dismiss();
JSONObject jsonResponse = null;
try
{
jsonResponse = new JSONObject(result);
JSONArray jsonMainNode = jsonResponse.optJSONArray("AccommoAddress");
/*********** Process each JSON Node ************/
int lengthJsonArr = jsonMainNode.length();
for(int i=0; i < lengthJsonArr; i++)
{
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
/******* Fetch node values **********/
String name = jsonChildNode.optString("Street");
String number = jsonChildNode.optString("City");
String date_added = jsonChildNode.optString("Longitude");
String lat = jsonChildNode.optString("Lattitude");
System.out.print("Street"+ name + "City" +number+ "Long" + date_added+" Lat" + lat);
Toast.makeText(MapsActivity.this, date_added + name + number + lat, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(MapsActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
"AccommoAddress" is a JSONObject not a JSONArray. So instead of this..
JSONArray jsonMainNode = jsonResponse.optJSONArray("AccommoAddress");
Try this ..
/*String Accommo = jsonResponse.getString("AccommoAddress");
JSONObject AccomoAddress = new JSONObject(Accommo);*/
//simplifying the above code
JSONObject Accomoaddress = jsonResponse.optJSONObject("AccomoAddress");
String name = AccomoAddress.getString("Street");
String number = AccomoAddress.getString("City");
String date_added = AccomoAddress.getString("Longitude");
String lat = AccomoAddress.getString("Lattitude");
Your response is a JSONArray, not a JSONObject, similarly, AccommoAddress is a JSONObject, not a JSONArray. So you need to change the lines near the top to the following:
JSONArray jsonResponse = null;
try
{
jsonResponse = new JSONArray(result);
JSONObject jsonMainNode = jsonResponse.optJSONObject("AccommoAddress");
This question already has an answer here:
JSON array parsing in android
(1 answer)
Closed 7 years ago.
I have a JSON response in this format:
{
"success": true,
"categories": [{
"id": "774",
"name": "1"
}, {
"id": "774",
"name": "1"
}]
}
And I am parsing it like this:
try {
JSONObject obj = new JSONObject(response);
String success = String.valueOf(obj.getBoolean("success"));
JSONArray arr = obj.getJSONArray("categories");
//loop through each object
for (int i=0; i<arr.length(); i++) {
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("id");
Toast.makeText(getApplicationContext(),name, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
But I only get the value of success. What I'm doing wrong here?
Parse as below -
JSONObject obj = new JSONObject(json);
String success = obj.getString("success");
JSONArray arr = obj.getJSONArray("categories");
//loop through each object
for (int i=0; i<arr.length(); i++) {
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("id");
}
correct json key
JSONArray arr = obj.getJSONArray("checkouts");
replace by:
JSONArray arr = obj.getJSONArray("categories");
DO like this,
if (!result.equalsIgnoreCase("")) {
try {
JSONObject _jsonObject = new JSONObject(result);
boolean json = false;
json = _jsonObject.getBoolean("Status");
JSONArray jsonArray1 = _jsonObject.getJSONArray("categories");
for (int i=0; i<jsonArray1.length(); i++) {
JSONObject jsonObject = jsonArray1.getJSONObject(i);
String name = jsonObject.getString("name");
String id = jsonObject.getString("id");
}
} catch (Exception e) {
Utils.printLoge(5, "error parse json", "--->" + e.getMessage());
return "ERROR";
}
}
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 7 years ago.
{
"response":
{
"Category":
[
"Restaurant",
"Salon",
"Spa",
"Dry Cleaners",
"Car Spa",
"Hospitals"
]
}
}
You can do like this-
try {
String json = <Your Json String>;
JSONObject response = new JSONObject(json);
JSONArray category = response.getJSONArray("Category");
for (int i = 0; i < category.length(); i++) {
Log.d("categories are", category.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this , it can be Pseudo Code
String json = <YourJSon String>
JSONObject object = new JSONObject(json);
JSONArray array = object.getJSONArray("Category");
for(int i =0; i<array.size();i++){
String s = array.get(i).getString();
}
I am trying to get a list of available numbers from the following json object, using the class from org.json
{
"response":true,
"state":1,
"data":
{
"CALLERID":"81101099",
"numbers":
[
"21344111","21772917",
"28511113","29274472",
"29843999","29845591",
"30870001","30870089",
"30870090","30870091"
]
}
}
My first steps were, after receiving the json object from the web service:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
Now, how do I save the string array of numbers?
use:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
you need to use JSONArray to pull data in an array
JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
Assuming that you are trying to get it in a javascript block, Try something like this
var arrNumber = jsonData.numbers;
My code is for getting "data":
public void jsonParserArray(String json) {
String [] resultsNumbers = new String[100];
try {
JSONObject jsonObjectGetData = new JSONObject(json);
JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
for (int i = 0; i < jsonArray.length(); i++) {
resultsNumbers[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.toString());
}
}