JSON PARSING IN ANDROID? - android

I need to parse Json in Android, as a newbie in Json as well as in Android I am unable to do so here is the json String:
[
{
"chapter": "1. General",
"lessons": [
{
"lesson": "1.1 "
},
{
"lesson": "1.2"
},
{
"lesson": "1.3"
}
]
},
{
"chapter": "2.emergencies"
}
]
Here I just want to get the lessons array data. So any help will really be appreciated. Thanks

JSONObject jObject = new JSONObject(jsonString);
JSONObject chapObject = jObject.getJSONObject("chapter");
Log.d("Chapt", chapObject.getString("chapter"));
JSONArray lessonArray = popupObject.getJSONArray("lessons");
for (int i = 0; i < 3; i++) {
Log.d("Name", lessonArray.getJSONObject(i).getString("lesson").toString());
Log.d("Value", lessonArray.getJSONObject(i).getString("onclick").toString());  
}     
JSONObject chap2Object = jObject.getJSONObject("chapter");
Log.d("Chapt2", chapObject.getString("chapter"));

Use JSONObject and/or JSONArray. They can be created directly from strings, eg:
JSONObject json = new JSONObject(string);

Android includes a JSON parser: http://developer.android.com/reference/org/json/package-summary.html
String json = "{ ... }";
JSONObject obj = new JSONObject(json);
JSONArray array = obj.getJSONArray("lessions");
for (int i = 0; i < array.length(); i++) {
String lession = array.getJSONObject(i).getString("lession");
}

Related

Android get inner json array from json array

Hello guys I have a tricky problem which I've been solving for hours.
This is my JSON response:
{
"DATA": [
{
"Name": "Aha",
"ListData": [
{
"ID": 1
},
{
"ID": 2
},
{
"ID": 3
}
]
}
]
}
This is what I've done so far:
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray jsonArray = jsonObj.getJSONArray("DATA");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject data = jsonArray.getJSONObject(i);
JSONArray arr = data.getJSONArray(Constants.LIST_DATA);
for(int j = 0; j < arr.length(); j++) {
JSONObject innerData = arr.getJSONObject(i);
int id = innerData.getInt(Constants.ID);
item = new HashMap<>();
item.put(Constants.ID, id);
itemList.add(item);
}
}
} catch(JSONException e) {
Log.e("JSONException", "" + e.toString());
}
But I'm only getting the correct size (3) of the array but I'm getting the same ID which 1. What seems to be wrong with my code? Any help would greatly appreciated. Thanks!
Result is something like this:
ID=1,ID=1,ID=1
Whereas my expected is:
ID=1,ID=2,ID=3
You should have used
JSONObject innerData = arr.getJSONObject(j);
you're doing second loop with j=0 and you're trying to getting JSONString from first loop.

Android parsing json object inside array

I want parse details of "name" and "id" here is my json and android code
{
"main": {
"details": [
{
"name": "name1",
"id": "id1"
},
{
"name": "name2",
"id": "id2"
}
]
}
}
and my code is:
try {
JSONObject jsono = new JSONObject(url);
SONArray jarray = jsono.getJSONArray("main");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Actors actor = new Actors();
actor.setLink(object.getString("name"));
actor.setImage(object.getString("id"));
actorsList.add(actor);
}
return true;
}
I want out put "id" and "name"
try Like this:
JSONObject totalObject = new JSONObject(result);
JSONObject mainObject = totalObject.getJSONObject("main");
JSONArray jsonArray = mainObject.getJSONArray("details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
Actors actor = new Actors();
actor.setLink(object.getString("name"));
actor.setImage(object.getString("id"));
actorsList.add(actor);
}
Follow by this way:
try {
JSONObject jsono = new JSONObject(url);
SONArray jarray = jsono.getJSONArray("main");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
String name = object.getString("name");
String id = object.getString("id");
}
return true;
}
main is JSONObject, details is JSONArray, so the correct code to declare jarray is the following:
JSONArray jarray = jsono.getJSONObject("main").getJSONArray("details");
You have a JSONObject "main", containing a JSONOArray "details". This means you have to call getJSONObject("main").getJSONArray("details");
If you are going to such parsing multiple times, I'd recommend you to take a look at Gson - it is much easier with it. You have to set a SerializableName attribute to the link and image fields of the Actor class and Gson will take care to map them with the values from the json.

JSON array can't be converted into JSON object

I'm working on json parsing project. The error is showing as json array cant be converted into json object. Here is the json view.
{
"State Name": [
[
{
"state_name": "New South Wales (NSW)"
}
],
[
{
"state_name": "Western Australia (WA)"
}
]
]
}
java code is:
try {
JSONArray statesnames = json.getJSONArray(TAG_STATE_NAME);
Toast.makeText(getApplicationContext(), ""+statesnames.length(), 3000).show();
// looping through All Contacts
for(int i = 0; i < statesnames.length(); i++){
JSONObject c = statesnames.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
}
If you're trying to send both arrays in one response you could do that:
[
[{"state_name":"New South Wales (NSW)"}],
[{"state_name":"Western Australia (WA)"}]
]
Take your JSONObject first as
JSONObject result= new JSONObject(json);
JSONArray arr= result.getJSONArray("State Name");
Whole JSON string is a JSONArray:
getJSONArray(0) contains JSONObjects with state_name as below:
for (int i = 0; i < arr.length(); i++) {
JSONArray firstArry = arr.getJSONArray(i);
for (int j = 0; j < firstArry.length(); j++) {
JSONObject secondarr= firstArry.getJSONObject(j);
System.out.println("state_name result: "+secondarr.getString("state_name"));
}
}
Try this..
{
"State Name":[ --> JSONArray
[ --> JSONArray
{ --> JSONObject
"state_name":"New South Wales (NSW)"
}
],
[
{
"state_name":"Western Australia (WA)"
}
]
]
}
Example :
JSONObject json = new JSONObject(response);
JSONArray statesnames = json.getJSONArray("State Name");
for (int i1 = 0; i1 < statesnames.length(); i1++) {
JSONArray jsonarray = statesnames.getJSONArray(i1);
for (int i2 = 0; i2 < jsonarray.length(); i2++) {
JSONObject jsonobject = jsonarray.getJSONObject(i2);
System.out.println("state_name : "+jsonobject.getString("state_name"));
}
}

How to create Json using JsonArray and JsonObject

I want to create a Json structure which is actually a JsonArray inside a JsonObject.
The sample structure is:
1.
{
“req”: [
{
“ctrlId”:”txt1”
},
{
“ctrlId”:”txt2”
}
]
}
2.
{
“req”: [
{
“ctrlId”:”txt1”,
“val” : “val1”
},
{
“ctrlId”:”txt2”,
“val” : “val2”
}
]
}
But i am not able to get it..Any help is appreciated..
JSONObject obj = new JSONObject();
JSONArray req = new JSONArray();
JSONObject reqObj = new JSONObject()
reqObj.put( "ctrlId", "txt1" );
req.put( reqObj );
reqObj = new JSONObject();
reqObj.put( "ctrlId", "txt2" );
req.put( reqObj );
obj.put( "req", req );
The final object is obj
I have this array
{
"result": "success",
"countryCodeList":
[
{"countryCode":"00","countryName":"World Wide"},
{"countryCode":"kr","countryName":"Korea"}
]
}
Here below I am fetching country details, so I have used valArray.getJSONArray(1)
You can use valArray.getJSONArray(0)
JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
JSONArray valArray1 = valArray.getJSONArray(1);
valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");
int len = valArray1.length();
for (int i = 0; i < valArray1.length(); i++) {
Country country = new Country();
JSONObject arr = valArray1.getJSONObject(i);
country.setCountryCode(arr.getString("countryCode"));
country.setCountryName(arr.getString("countryName"));
arrCountries.add(country);
}
you can use GSON for doing this parsing. it will make your life simple.
you can have a look at my answers in this SO question

How do I pull the string array from this json object?

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());
}
}

Categories

Resources