my tryouts but its give me error
My JSON:
{
"status":true,
"message":"Front About Found",
"data":{
"FrontAbout":{
"ABOUT_ID":"1",
"CONTENT":"and the arts. ",
"SCHOOL_NAME":"The New School",
"ADDRESS":" Western Corridor",
"LOGO":"14643339731.png"
}
}
}
My Sample code:
{
JSONArray jsonarray = jObj.getJSONArray("data");
for (int z = 0; z < jsonarray.length(); z++) {
JSONObject jobj = jsonarray.getJSONObject(z);
JSONObject jobj_FrontAbout = jobj.getJSONObject("FrontAbout");
school = jobj_FrontAbout.getString("SCHOOL_NAME");
add = jobj_FrontAbout.getString("ADDRESS");
phone = jobj_FrontAbout.getString("CONTACT_NO");
email = jobj_FrontAbout.getString("EMAIL");
cont = jobj_FrontAbout.getString("CONTENT");
image = jobj_FrontAbout.getString("LOGO");
setData();
loadingView.dismiss();
}
loadingView.dismiss();
if (loadingView != null && loadingView.isShowing()) {
loadingView.dismiss();
}
}
its give me error " JSONObject can not to be converted to JSONArray "
i want to simple set in textview not in array
please give me solution
Here are you are parsing for "data" as a JSOnArray Although "data" is a JSONObJect .
JSON nodes will start with a square bracket or with a curly bracket. The difference between [ and { is, the square bracket ([) represents starting of an JSONArray node whereas curly bracket ({) represents JSONObject.
{
JSONObject dataJsonObject = jObj.getJSONObject("data");
for (int z = 0; z < dataJsonObject.length(); z++) {
JSONObject jobj = jsonarray.getJSONObject(z);
JSONObject jobj_FrontAbout = jobj.getJSONObject("FrontAbout");
school = jobj_FrontAbout.getString("SCHOOL_NAME");
add = jobj_FrontAbout.getString("ADDRESS");
phone = jobj_FrontAbout.getString("CONTACT_NO");
email = jobj_FrontAbout.getString("EMAIL");
cont = jobj_FrontAbout.getString("CONTENT");
image = jobj_FrontAbout.getString("LOGO");
setData();
loadingView.dismiss();
}
loadingView.dismiss();
if (loadingView != null && loadingView.isShowing()) {
loadingView.dismiss();
}
}
HopeFully You will get rid of your error . CHEERS
you can parse like this.
JSONObject jobj = jsonarray.getJSONObject(z);
status=jobj.getBoolean("status");
message=jobj.getString("message");
JSONObject dataObject = jobj.getJSONObject("data");
JSONObject frontAboutObject = dataObject.getJSONObject("FrontAbout");
school = frontAboutObject.getString("SCHOOL_NAME");
add = frontAboutObject.getString("ADDRESS");
phone = frontAboutObject.getString("CONTACT_NO");
email = frontAboutObject.getString("EMAIL");
cont = frontAboutObject.getString("CONTENT");
image = frontAboutObject.getString("LOGO");
Try this:
JSONObject jsonObj= jObj.getJSONObject("your json string");
JSONObject jsonInnerObj= jsonObj.getJSONObject("data");
Related
Iwant to access a jsonArray data in a jsonObjet response code that comes from server. here is my response json
{
event_id: "32",
event_title: "امیر",
event_description: "تست امیر",
event_image: "2_1550507094.jpg",
event_hikers_amount: "9",
event_private: "0",
event_date_start: "17/2/2019",
event_date_end: "21/2/2019",
event_time_start: "21:54",
event_time_end: "12:54",
event_locations_array:
"[
{"latitude":37.58728984572849,"longitude":45.10016608983278},
{"latitude":37.57651702299841,"longitude":45.0880378112197},
{"latitude":37.5753956777439,"longitude":45.1045374199748},
{"latitude":37.564077382844964,"longitude":45.094508975744255},
{"latitude":37.55829758877768,"longitude":45.08105669170619},
{"latitude":37.53919984571198,"longitude":45.09874418377876}
]",
event_latitude_location: "37.587289845728",
event_longitude_location: "45.100166089833",
event_status: "1",
event_users_id: "2"
}
I want to parse "event_locations_array" and what what i done :
#Override
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
description = jsonObject.getString("event_description");
people_joined = jsonObject.getString("event_hikers_amount");
date_start = jsonObject.getString("event_date_start");
date_end = jsonObject.getString("event_date_end");
time_start = jsonObject.getString("event_time_start");
time_end = jsonObject.getString("event_time_end");
privacy = jsonObject.getString("event_private");
JSONArray jsonArray = jsonObject.getJSONArray("event_locations_array");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
JSONObject lat = points.getJSONObject("latitude");
JSONObject lang = points.getJSONObject("longitude");
}
setTextView();
} catch (JSONException e) {
e.printStackTrace();
}
}
here i can't take that jsonArray. what i did wrong here let me know . I'm a little confused
thanks in advance
Fast solution
If you can not change the JSON generation, simply use this code:
#Override
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
description = jsonObject.getString("event_description");
people_joined = jsonObject.getString("event_hikers_amount");
date_start = jsonObject.getString("event_date_start");
date_end = jsonObject.getString("event_date_end");
time_start = jsonObject.getString("event_time_start");
time_end = jsonObject.getString("event_time_end");
privacy = jsonObject.getString("event_private");
// 1 - fix string to array conversion
JSONArray jsonArray = new JSONArray(jsonObject.getString("event_locations_array"));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
// 2 - the fields as double
double lat = points.getDouble("latitude");
double lang = points.getDouble("longitude");
}
setTextView();
} catch (JSONException e) {
e.printStackTrace();
}
}
Detailed solution
There are two errors:
The event_locations_array : "[[{"latitude":37.58728984572849].." contains a string. For the value that it's contains I think it must be generated as array (without the initial and final ")
After fix the first problem, you are trying to extract as object the properties latitude and longitude, but they are attributes. So change in your code:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
JSONObject lat = points.getJSONObject("latitude");
JSONObject lang = points.getJSONObject("longitude");
}
With
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
double lat = points.getDouble("latitude");
double lang = points.getDouble("longitude");
}
To get Values of event_locations_array use following code:
JSONArray jsonArray = jsonResponse.getJSONArray("event_locations_array");
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String latitude = jsonObject.getString("latitude");
String longitude = jsonObject.getString("longitude");
}
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 :)
The is my JSON string .
{
"server_response":
{
"source_response" :
[
{"stoppage_name":"sealdah","bus_no":"43#230#234/1#30_A"}
] ,
"destination_response" :
[
{"stoppage_name":"howrah","bus_no":"43#234/1#30_A"}
]
}
}
I think there would be a '[' after "server_response" , but not sure .
I am trying to retrieve the data but the code is not working .
try {
jsonObject = new JSONObject(json_string);
jsonArray = jsonObject.getJSONArray("server_response");
int count=0 ;
String stoppage,busno;
while(count<1)
{
JSONArray JA = jsonArray.getJSONArray(0);
JSONObject JO = JA.getJSONObject(count);
stoppage = JO.getString("stoppage_name");
busno = JO.getString("bus_no");
Toast.makeText(getApplicationContext(),"Stoppage ="+ stoppage+" Bus no =" +busno, Toast.LENGTH_LONG).show();
count ++;
}
} catch (JSONException e) {
e.printStackTrace();
}
Where I am making wrong . I am new to JSON and Android.
jsonObject = new JSONObject(json_string);
jsonServerObject = jsonObject.getJSONObject("server_response");
jsonSourceArray = jsonServerObject.getJSONArray("source_response");
jsonDestinationArray = jsonServerObject.getJSONArray("destination_response");
//Iterate your 2 arrays
server_response is not a JSONArray, it's a JSONObject. Because array have numeric key, not string.
i have already answer this type of question .. what you have to do .. use multiple for loop to getting value inside array under array.
this is srceen shots
JSONArray objJson = new JSONArray(strJSONData);
System.out.println("AppUserLogin:"+objJson);
// Parsing json
if(arrJson.length()>0)
{
for (int i = 0; i < objJson.length(); i++) {
try {
JSONObject objprod = arrJson.getJSONObject(i);
HashMap<String, String> MaplistTemp = new HashMap<String, String>();
MaplistTemp.put("replyCode",
objprod.getString("replyCode"));
MaplistTemp.put("replyCode",
objprod.getString("replyCode"));
JSONArray objproduct_var = new JSONArray(objprod.getString("LearningStandards"));
if ((objproduct_var.length()) > 0) {
for (int k = 0; k < objproduct_var.length(); k++) {
JSONObject objprodvar = objproduct_var
.getJSONObject(k);
MaplistTemp
.put("1",
objprodvar
.getString("1"));
MaplistTemp
.put("2",
objprodvar
.getString("2"));
MaplistTemp
.put("3",
objprodvar
.getString("3"));
MaplistTemp
.put("4",
objprodvar
.getString("4"));
}
}
// sub_categorys_details.add(sub_cat_det);
medpostList.add(MaplistTemp);// adding to final hashmap
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Try this , and {} = object [] = array
try{
jsonObject = new JSONObject(json_string);
injsonObject = jsonObject.getJSONObject("server_response");
jsonArray = injsonObject.getJSONArray("source_reponse");
}catch(Exception e){}
sonArray = jsonObject.getJSONArray("server_response");
Here you are trying to access server_response as an array. It isn't an array. "server_response" is the key to the nested object:
{
"source_response" :
[
{"stoppage_name":"sealdah","bus_no":"43#230#234/1#30_A"}
] ,
"destination_response" :
[
{"stoppage_name":"howrah","bus_no":"43#234/1#30_A"}
]
}
See? Thats not an array, tis a JSON object with two keys, each of them have an array as value.
I'm not very familiar with android or java but in plain old javascript you could try something like:
var sourceResp = jsonObject.server_response.source_response;
or
var sourceResp = jsonObject["server_response"]["source_response"];
That will produce an array with one item in it.
I hope this can get you going.
Here is my JSON file:
{
"server_response": [{
"bmw": "",
"mercedes": "",
"honda": "civic",
"toyota": "corolla",
"gmc": "",
"chevy": ""
}]
}
Here is my Android code:
try {
JSONObject jsonResponse = new JSONObject(JSON_STRING);
JSONArray jsonMainNode = jsonResponse.optJSONArray("server_response");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
//this is the problem here. How can I get JSON that has a result like Honda and Toyota?
{variable name} = jsonChildNode.optString("{problem is here}");
CarsModel carsModel new CarsModel( {variable name} {variable name} );
}
} catch (JSONException e) {
e.printStackTrace();
}
The problem is in the Android code as you can see. I want it to only get the JSON that is not empty, for example honda and toyota.
How can replace {variable name} with in this case, honda and then replace {problem is here} with the json result that's not blank?
I also want to add the {variable names} into the CarsModel carsModel new CarsModel( {variable name} {variable name} );.
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
// Get the keys in the JSON object
Iterator<?> keys = jsonChildNode.keys();
while (keys.hasNext()) {
// Get the key
String key = (String)keys.next();
String objValue = jsonChildNode.getString(key);
// check if empty
if (!objValue.isEmpty()) {
CarsModel carsModel new CarsModel(objValue);
}
}
}
This is what you need to do. The data you want is a json object inside a jsonArray
JSONObject jsonResponse = new JSONObject(JSON_STRING);
JSONArray jsonMainNode = jsonResponse.optJSONArray("server_response");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
variableX = jsonChildNode.getString("toyota");
variableY = jsonChildNode.getString("honda");
variableZ = jsonChildNode.getString("xyz");
// CarsModel thing using var x, y, z...
}
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");
}