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 :)
Related
I have know to parser JSON array in single array but how to pass multiple JSON array and set it value to require
{"scode":"200","all_menu":[{"app_menu_id":"67","app_menu_name":"Demograpics","all_sub_menu":[{"app_menu_id":"67","app_sub_menu_id":"47","app_sub_menu_name":"\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/8451504072003.jpg"}],"sub_menu":"true"},{"app_menu_id":"68","app_menu_name":"Lyrics","all_sub_menu":[{"app_menu_id":"68","app_sub_menu_id":"48","app_sub_menu_name":"Music","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4681504072092.jpg"}],"sub_menu":"true"},{"app_menu_id":"69","app_menu_name":"Adult","all_sub_menu":[{"app_menu_id":"69","app_sub_menu_id":"49","app_sub_menu_name":"Double
Meaning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/9931504072151.jpg"}],"sub_menu":"true"},{"app_menu_id":"70","app_menu_name":"Emotions","all_sub_menu":[{"app_menu_id":"70","app_sub_menu_id":"50","app_sub_menu_name":"Love","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/7611504072164.jpg"}],"sub_menu":"true"},{"app_menu_id":"71","app_menu_name":"Wishes","all_sub_menu":[{"app_menu_id":"71","app_sub_menu_id":"51","app_sub_menu_name":"Good
Morning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/5171504072183.jpg"}],"sub_menu":"true"},{"app_menu_id":"72","app_menu_name":"Among Friend","all_sub_menu":[{"app_menu_id":"72","app_sub_menu_id":"52","app_sub_menu_name":"Friendship","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4411504072205.jpg"}],"sub_menu":"true"},{"app_menu_id":"73","app_menu_name":"Jokes","all_sub_menu":[{"app_menu_id":"73","app_sub_menu_id":"53","app_sub_menu_name":"Santa
Banta","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4331504072225.jpg"}],"sub_menu":"true"},{"app_menu_id":"74","app_menu_name":"Featured","all_sub_menu":[{"app_menu_id":"74","app_sub_menu_id":"54","app_sub_menu_name":"Ganpati
Bappa","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4771504072247.jpg"}],"sub_menu":"true"}]}
Suppose "response" is your JSONResponse
JSONObject jsonObject = new JSONObject(response);// This is used to get jsonObject from response
String sCode=jsonObject.optString("scode"); // This is how you can parse string from jsonObject
JSONArray allmenuArray=jsonObject.optJSONArray("all_menu"); //This is how you can parse JsonArray from jsonObject
for(int i=0;i<allmenuArray.length();i++){
JSONObject objectJson=allmenuArray.optJSONObject(i);//This is how you can parse jsonObject from jsonArray
}
Like This you can parse all your jsonObject and jsonarray. Just follow these steps you can easily parse your full JSONResponse
Try this.
try {
JSONObject jsonObject = new JSONObject("JSONResponse");
String scode = jsonObject.optString("scode");
JSONArray allmenuArray = jsonObject.optJSONArray("all_menu");
for (int i = 0; i < allmenuArray.length(); i++) {
JSONObject objectJson = allmenuArray.optJSONObject(i);
boolean sub_menu = objectJson.getBoolean("sub_menu");
String app_menu_id = objectJson.getString("app_menu_id");
String app_menu_name = objectJson.getString("app_menu_name");
JSONArray all_sub_menu = objectJson.getJSONArray("all_sub_menu");
for (int j = 0; j < all_sub_menu.length(); j++) {
JSONObject data = allmenuArray.optJSONObject(j);
Log.e("app_menu_id", data.getString("app_menu_id"));
Log.e("app_sub_menu_id", data.getString("app_sub_menu_id"));
Log.e("app_sub_menu_name", data.getString("app_sub_menu_name"));
Log.e("app_sub_menu_image", data.getString("app_sub_menu_image"));
}
}
} catch (JSONException e) {
Log.e("ERROr", e.toString());
}
I have a json with colon between the strings, and I'm not sure how can I parse it. I know that I don't have an array in the json, but I'm not sure how can I get the values...
{
"config": {
"network": {
"hni:21407" : "num:[INTNUM]",
"hni:311490" : "num:044[INTNUM]"
}
}
}
This is what I'm trying, but I never go through the loop for, and not really sure if I need it.
JSONObject obj = new JSONObject(netWorkJson);
String arr = obj.optString("network");
for(int i = 0; i < arr.length(); i++) {
String hni = obj.getString("hni");
String num = obj.getString("num");
}
Thanks in advance
You first need to parse the inner json object "network", after that you can loop over it's keys and get the values for them one by one:
private void parseJSON(String netWorkJson) throws JSONException {
JSONObject obj = new JSONObject(netWorkJson);
JSONObject config = obj.getJSONObject("config");
JSONObject network = config.getJSONObject("network");
Iterator<?> keys = network.keys();
while(keys.hasNext()) {
String key = (String) keys.next();
String value = network.getString(key);
}
}
Beauty of this is that it will also work if you had 100 hni values for example, and that you don't have to get them one by one.
network is JSONObject instead of JSONArray, so no need to use for-loop for getting value from it.just use do it as:
JSONObject obj = new JSONObject(netWorkJson);
// get network JSONObject from obj
JSONObject network=obj.getJSONObject("network");
// get both values from network object
String strHni=network.optString("hni:21407");
String strNum =network.optString("hni:311490");
JSONObject message = new JSONObject(config);
String value=message.getJSONObject("network").getString("hni:21407")
Try This
try {
JSONObject jsonObject = new JSONObject("config");
JSONArray jsonArray = jsonObject.getJSONArray("network");
for(int i =0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String hni21407 = jsonObject1.getString("hni:21407");
String hni311490 = jsonObject1.getString("hni:311490");
}
} catch (JSONException e) {
e.printStackTrace();
}
My application connects to the web and retrieves a JSON file. I am having some issues retrieving the data that I need from this file. Here is the link to the JSON file: http://api.wunderground.com/api/f9d9bc3cc3834375/forecast/q/CA/San_Francisco.json
Here is a snippet of what it looks like:
I would like to get the value of the "period" variable in the first JSon object within the "forecastday" array, which should be 0. Here is how i'm looking at this. "forecastday" is the array, within that there are a number of JSon objects, each containing variables like "period","icon"....."pop".
In my code, I attempt to get the JSON array "forecastday" and then get the first Json object of the array, I then retrieve the value of "period" in that object and set it in a TextView:
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
progressBar.setVisibility(View.GONE);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray contacts = jsonObj.getJSONArray("forecastday");
JSONObject c = contacts.getJSONObject(0);
String period = c.getString("period");
responseView.setText(period);
} catch (JSONException e) {
e.printStackTrace();
}
}
When I run the code, nothing is being retrieved. I'm new to working with JSon and am wondering if I'm looking at this wrong. Please help.
Your "forecastday" JSONArray is inside the "txt_forecast" JSONObject that is inside the "forecast" JSONObject of your response, so you have to extract your JSONArray from this JSONObject and not from the root JSON response :
try {
JSONObject jsonObj = new JSONObject(response);
--> JSONObject forecatsObj = jsonObj.getJSONObject("forecast");
--> JSONObject txtForecatsObj = forecatsObj.getJSONObject("txt_forecast");
JSONArray contacts = txtForecatsObj.getJSONArray("forecastday");
...
You were close but it is incorrect, try like this:
JSONObject jsonObj = new JSONObject(response);
JSONObject forecast = jsonObj.getJSONObject("forecast");
JSONObject txtForecast = forecast.getJSONObject("txt_forecast");
JSONArray forecastDay = txtForecast.getJSONArray("forecastday");
//parse the first period value
String period = forecastDay.getJSONObject(0).getString("period");
responseView.setText(period);
Try this code :
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject forecastObj = jsonObj.getJSONObject("forecast");
JSONObject txt_forecastObj = forecastObj.getJSONObject("txt_forecast");
JSONArray foracastdayArray = txt_forecastObj.getJSONArray("foracastday");
JSONObject oOjb0 = foracastdayArray.getJSONObject(0);
String period = oOjb0.getString("perioed");
}catch (Exception e){
}
This code works for me fine:
JSONObject jsonResponse = new JSONObject(responce);
JSONArray jsonMainNode = jsonResponse.optJSONArray("forecastday");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String period = jsonChildNode.optString("period");
responceView.setText(period);
}
l get string with answer from server. l want to do it JSONObject.
l do
JSONObject jsonObj = new JSONObject(json);
in json has
"{"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405","translit":"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":1}"
but jsonObj has
"{"sentences":[{"src_translit":"RẑRg̀RoRẐ","orig":"�ѕ�ґ��Ѕ","trans":"RѕRґReRЅ","translit":""}],"server_time":1,"src":"ru"}"
so how l can get value from "trans":"RѕRґReRЅ"?
PS RѕRґReRЅ it's "own" in normal charset
If you already have the jsonObj you just have to do:
try {
// Getting Array of Sentences
sentences = json.getJSONArray("sentences");
// looping through All Contacts
for(int i = 0; i < sentences.length(); i++){
JSONObject c = sentences.getJSONObject(i);
// get the value from trans
String trans = c.getString("trans");
//now you should save this string in an array
}
} catch (JSONException e) {
e.printStackTrace();
}
try this
JSONObject jsonObj = new JSONObject(json);
JSONArray sentences = jsonObj.getJSONArray("sentences");
for(int i=0;i<sentences.length();i++){
JSONObject number = sentences.getJSONObject(i);
String transValue = number.getString("trans");
}
First you have to remove single quote form starting and from ending of your JSON string (json), by this your JSON will be a valid json for this you have to do like :
json= json.substring(1, json.length()-1);
After that you do like this :
JSONObject oJsonObject = new JSONObject(json);
JSONArray oJsonArray = oJsonObject.getJSONArray("sentences");
for(int i=0; i<oJsonArray.length(); i++)
{
JSONObject oJsonObject1 = oJsonArray.getJSONObject(i);
String transValue = oJsonObject1 .getString("trans");
}
I'm trying to get data once user logged in successfully but I never get any of results, what I am doing is next:
// response is my request to server
JSONObject obj = new JSONObject(response);
Log.d("RESPONSE",obj.toString());
so in log I do see values, like:
04-19 11:28:16.729: D/RESPONSE(3162): {"data":[{"loses":3,"username":"benedict","level":1,"strength":15,"experience":null,"gold":10,"password":"benedict","intelligence":5,"agility":10,"wins":5}],"status":true}
but once I try to read username for example like this:
String username = obj.getString("username");
The code above ^ gives me nothing in my string..
Any help how I can retrieve data from JSONObject? Thanks!
That is because the username is present in the data object, which happens to be an JSONArray. Get the data array from the response object, traverse through each JSONObject in the array, and from each object, extract your username.
Something like this:-
JSONObject obj = new JSONObject(response);
JSONArray data = obj.getJSONArray("data");
for(int i=0;i<data.length();i++){
JSONObject eachData = data.getJSONObject(i);
System.out.println("Username= "+ eachData.getString("username"));
}
your field username is in array data. To access into this try :
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("data");
for(int i = 0; i < array.length(); ++i){
JSONObject data = array.getJSONObject(i);
String username = data.getString("username");
}
You need to first get JSONArray which is data :
JSONArray data = null;
data = json.getJSONArray("data");
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
String username = c.getString("username");
}
You can get idea about parsing JSON from HERE
Try this...
try {
JSONObject object = new JSONObject(response);
JSONArray Jarray = object.getJSONArray("data");
for (int i = 0; i < Jarray.length(); i++) {
JSONObject Jasonobject = Jarray.getJSONObject(i);
String loose= Jasonobject.getString("loses");
String username=Jasonobject.getString("username");
.......
........
}
} catch (JSONException e) {
Log.e("log_txt", "Error parsing data " + e.toString());
}