JSON inner query in android [duplicate] - android

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
"BRAZIL": {
"channel": "brazil_ch",
"city": {
"Belo Horizonte": "belohorizonte_ch_org",
"Brasilia": "brasilia_ch_org",
"Curitiba": "curitiba_ch_org",
"Fortaleza": "fortaleza_ch_org",
"Porto Alegre": "portoalegre_ch_org",
"Recife": "recife_ch_org",
"Rio De Janeiro": "rio_ch_org",
"Salvador, Bahia": "salvador_ch_org",
"Sao Paulo": "saopaulo_ch_org"
}
}
This is my JSON. I need to retrieve the channel value in Brazil and also city values. Please help me.
Thanks in Advance.

JSONObject jsonObject = new JSONObject(jsonString);
String channel = jsonObject.getJSONObject("BRAZIL").getString("channel");
JSONArray jsonArray = jsonObject.getJSONObject("BRAZIL").getJSONArray("city");
int length = jsonArray.length();
String[] cities = new String[length];
for (int i = 0; i < length; i++) {
String city = jsonArray.getString(i);
cities[i] = city;
}

Related

How do i parse json object and json array in android? [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
I am developing an app using json api and want to implement following format api into my app. How do i implement this in my project using android studio 2.3?
{
"getdetailsasanas": [
{
"asanaid": 1,
"asananame": "Half easy gas release pose",
"duration": 1,
"imageurl": "http://www.yogapoint.com/iOS/images/half-easy-gas-release-pose.jpg",
"imageversion": 2,
"audiourl": "http://www.yogapoint.com/iOS/audio/half-easy-gas-release-pose.mp3",
"audioversion": 1,
"videourl": "NULL",
"videoversion": 0,
"StepsForAsana": [
{
"stepnumber": 1,
"stepdesc": "Step 1",
"stepimg": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step1.jpg",
"stepimgeversion": 0,
"imgethumbversion": 1,
"imgethumburl": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step1-tb.jpg"
},
{
"stepnumber": 2,
"stepdesc": "Step 2",
"stepimg": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step2.jpg",
"stepimgeversion": 0,
"imgethumbversion": 1,
"imgethumburl": "http://www.yogapoint.com/iOS/images/asana-step/half-easy-gas-release-pose-step2-tb.jpg"
}
]
}
]
}
You can use JSONObject its default supplied with android SDK:
String json = "some JSON";
JSONObject jsonObject = new JSONObject(json);
Than you can use methods like
getBoolean(String key)
getDouble(String key)
etc
Or you can use google gson library which is in my opinion better, but you will need to include it as dependency in gradle.
There is github for the Gson.
There is Gson API
Use Gson library . Best way to parse the responses.Your can user GsonFormat plugin to generate the model for response.
Have a look on gson. It's a easy serializer for json.
It's my pleasure to answer your question.
You can do like this.
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("getdetailsasanas");
for (int i = 0; i < jsonArray.length(); i++) {
String asanaid = jsonArray.getJSONObject(i).getString("asanaid");
String asananame = jsonArray.getJSONObject(i).getString("asananame");
String duration = jsonArray.getJSONObject(i).getString("duration");
String imageurl = jsonArray.getJSONObject(i).getString("imageurl");
String imageversion = jsonArray.getJSONObject(i).getString("imageversion");
String videourl = jsonArray.getJSONObject(i).getString("videourl");
String videoversion = jsonArray.getJSONObject(i).getString("videoversion");
JSONArray StepsForAsanaArray = jsonArray.getJSONObject(i).getJSONArray("StepsForAsana");
for (int j = 0; j < StepsForAsanaArray.length(); j++) {
String stepnumber = jsonArray.getJSONObject(j).getString("stepnumber");
String stepdesc = jsonArray.getJSONObject(j).getString("stepdesc");
String stepimg = jsonArray.getJSONObject(j).getString("stepimg");
String stepimgeversion = jsonArray.getJSONObject(j).getString("stepimgeversion");
String imgethumbversion = jsonArray.getJSONObject(j).getString("imgethumbversion");
String imgethumburl = jsonArray.getJSONObject(j).getString("imgethumburl");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Use like with this code:
JSONArray getdetailsasanas = new JSONArray(stringData);
for (int p = 0; p < getdetailsasanas.length(); p++) {
try {
JSONObject data = getdetailsasanas.getJSONObject(p);
JSONObject asanaid = data.getJSONObject("asanaid");
JSONObject stepnumber = data.getJSONObject("stepnumber");
JSONObject stepnumber = data.getJSONObject("stepnumber");
//get data from objetcs
String asananame = asanaid.getString("asananame");
int duration = asanaid.getInt("duration");
String imageurl = asanaid.getString("imageurl");
} catch (JSONException e) {
e.printStackTrace();
}
}

how to make JSON string from Normal String? [duplicate]

This question already has an answer here:
Converting from JSONArray to String then back again
(1 answer)
Closed 6 years ago.
JSONArray array = new JSONArray();
try
{
for (int i = 0; i < contactList.size(); i++){
JSONObject obj = new JSONObject();
String myString = contactList.get(i);
Log.e("Contact List",contactList.get(i));
String[] myarray = myString.split(",");
obj.put("is_private","0");
obj.put("expire_date","0000-00-00");
obj.put("name",myarray[0]);
obj.put("phone_no",myarray[1]);
obj.put("user_id", 1);
array.put(obj);
}
how we can convert json string to normal string ? please give me answer.
You can used toString function for this.
array.toString()
JSONObject obj = new JSONObject(yourstring);
String x = obj.getString("x");
Use this code, may help you.
JSONObject json = new JSONObject();
//put your values
json.toString();
clear your question please
JsonObject to String use:
json.toString();
String to JsonObject:
String data= "Json format data";
JsonObject obj=new JsonObject(data);

I want to convert below json array into array list [duplicate]

This question already has answers here:
Converting JSONarray to ArrayList
(23 answers)
Closed 6 years ago.
This is the link to my webserivce please check out
https://drive.google.com/open?id=0BwCPG2lI_UeCcXlLUGhibTJEdWs
this is image of the android code
android code
please help me
at first you should send JSONArray Data inside JSONObject then via below codes get and set your list
public void onResponse(String response) {
try {
JSONObject responseJsonObject = new JSONObject(response);
JSONArray arrayJsonObject = responseJsonObject.getJSONArray("items");
for (int i = 0; i < arrayJsonObject.length(); i++) {
JSONObject jsonObject = arrayJsonObject.getJSONObject(i);
//Or do somthing with Array Datas
}
}
}
if your array not have name :
JSONArray jsonArray = new JSONArray(String_Result);

How do I split these String Arraylist [{ } , {}, { }...] to strings or Integer? [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 8 years ago.
I have,
[{"GroupPosition":0,"ChildPosition":0},{"GroupPosition":0,"ChildPosition":1},{"GroupPosition":1,"ChildPosition":0}]
How do I split them into:
GroupPosition:
[0,0,1] or {0,0,1}
ChildPosition:
[0,0,0] or {0,0,0}
Your input is in json format. Consider my code is sample.
private static ArrayList<String> array_1 = new ArrayList<String>();;
String json = [{"GroupPosition":0,"ChildPosition":0},{"GroupPosition":0,"ChildPosition":1},{"GroupPosition":1,"ChildPosition":0}];
JSONArray leagues = new JSONArray(json);
for (int i = 0; i < leagues.length(); i++) {
JSONObject obj = leagues.getJSONObject(i);
array_1.add(obj.getString("GroupPosition"));
}

Convert a string to int array [duplicate]

This question already has answers here:
How to convert object array to string array in Java
(11 answers)
Closed 9 years ago.
I need to convert a string stored like,
textView.setText(myVar);
Here
myVar = "23,45,64,78";
I would like it to convert into array like below,
int[] xAxis = new int[]{23,45,64,78};
How can I achieve this? Thanks
Try this:
String arr = "[23,45,64,78]";
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i]);
} catch (NumberFormatException nfe) {};
}

Categories

Resources