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