I want to parse a JSON string in java and get the following values from the text mentioned below:
{
"columns": [
"id",
"name"
],
"rows": [
[
"124",
"LOREM"
],
[
"125",
"DOLOR"
],
[
"126",
"FUGIAT"
],
[
"127",
"IPSUM"
]
]
}
From every element, i want to get the value from array marked as rows. How do i do this in Jackson JSON for android?
Something like this :
List<String> rowsValues = new ArrayList<>(); // to store row String values in List
try {
JSONObject json = new JSONObject(data); //data is your json in String format
JSONArray array = json.getJSONArray("rows");
for (int index = 0 ; index < array.length(); index++) {
JSONArray currentArray = array.getJSONArray(index);
for (int i = 0 ; i < currentArray.length(); i++) {
rowsValues.add(array.getString(i));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Related
How I can get only the "name" string of every object under "fields" Array, of main Array index at 0 & then next index using loop or with something super idea
[
{
"name": "Bank1",
"fields": [
{
"name": "Email",
"slug": "email",
"type": "input"
},
{
"name": "City",
"slug": "city",
"type": "input"
},
{
"name": "Screenshot",
"slug": "screenshot",
"type": "file"
},
{
"name": "Full Name",
"slug": "full-name",
"type": "input"
}
],
"status": "Active"
},
{
"name": "Bank2",
"fields": [
{
"name": "Email",
"slug": "email",
"type": "input"
},
{
"name": "City",
"slug": "city",
"type": "input"
},
{
"name": "Screenshot",
"slug": "screenshot",
"type": "file"
},
{
"name": "Submitted Date",
"slug": "submitted-date",
"type": "calendar"
}
],
"status": "Active"
}
]
Output I want:
Email
City
Screenshot
Full Name
Means in the output, I have got index 0, first object array data...
What I have done yet
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String p_name = jsonObject.getString("name");
ArrayList<String> arr = new ArrayList<>();
JSONArray ja = jsonObject.getJSONArray("fields");
int len = ja.length();
for (int j = 0; j < len; j++) {
JSONObject json = ja.getJSONObject(j);
arr.add(json.getString("name"));
}
}}catch block...
this gives me all indexes name data i want only specific index data
My Current Output:
Email
City
Screenshot
Full Name
Email
City
Screenshot
Submitted Date
If you want to get specific index data then you should pass if condition in for loop.
EX: to get output of index 0 like,
Email
City
Screenshot
Full Name
Your code would be like below.
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
if(i==0){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String p_name = jsonObject.getString("name");
ArrayList<String> arr = new ArrayList<>();
JSONArray ja = jsonObject.getJSONArray("fields");
int len = ja.length();
for (int j = 0; j < len; j++) {
JSONObject json = ja.getJSONObject(j);
arr.add(json.getString("name"));
}
}
}}catch block...
int i=gotIndex;//here you can give your exact index that you want
JSONObject jsonObject = jsonArray.getJSONObject(i);
String p_name = jsonObject.getString("name");
ArrayList<String> arr = new ArrayList<>();
JSONArray ja = jsonObject.getJSONArray("fields");
int len = ja.length();
for (int j = 0; j < len; j++) {
JSONObject json = ja.getJSONObject(j);
arr.add(json.getString("name"));
}
here you used the loop you can use the json object out side the loop with using the index number i (jsonObject = jsonArray.getJSONObject(i))
I am new to android and json. I want to implement some logic where i can get the data from sub1 from json inside json inside another json.
Is there any way to get that data?
This is how my json file looks like.
{
"Aeronautical": [],
"Automobile": [],
"Civil": [],
"Computer": [
{
"sub1": [
{
"id": "1",
"name": "name1",
"year": "2019",
"url": "some_url"
},
{
"id": "2",
"name": "Name 2",
"year": "2018",
"url": "some url"
},
{
"id": "3",
"name": "Name 4",
"year": "2018",
"url": "some url"
}
]
}
]
}
sub1 is inside another array , so first you should get data from computer array
JSONObject obj = null;
try {
obj = new JSONObject(json);
JSONArray arr = obj.getJSONArray("Computer");
for (int i = 0; i < arr.length(); i++)
{
JSONObject obj_computers = arr.getJSONObject(i);
JSONArray sub1 = obj_computers.getJSONArray("sub1");
for (int j = 0; j < sub1 .length(); j++)
{
JSONObject sub1_data = sub1.getJSONObject(j);
Log.i("test" ,sub1_data.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
The Android SDK includes the org.json lib. You can use the JSONObject class.
String jsonString = /* obtain some JSON however you need to*/;
JSONObject root = new JSONObject(jsonString);
JSONObject sub1 = root.getJSONObject("sub1");
I omitted exception handling, but this is the basic idea.
I wrote code that takes a string that holds a JSON data. I'm sorting my JSON object array by ID. When I'm using my method I get this exception: "org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]".
What am I missing here and how to solve it?
private static void ResortJsonByUseCaseID( String jsonArrStr )
{
JSONArray jsonArr = new JSONArray(jsonArrStr);
JSONArray sortedJsonArray = new JSONArray();
List<JSONObject> jsonValues = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArr.length(); i++) {
jsonValues.add(jsonArr.getJSONObject(i));
}
java.util.Collections.sort( jsonValues, new java.util.Comparator<JSONObject>() {
private static final String KEY_NAME = "useCaseId";
#Override
public int compare(JSONObject a, JSONObject b) {
String valA = new String();
String valB = new String();
try {
valA = (String) a.get(KEY_NAME);
valB = (String) b.get(KEY_NAME);
}
catch (JSONException e) {
//do something
int tal = 9;
}
return valA.compareTo(valB);
}
});
for (int i = 0; i < jsonArr.length(); i++) {
sortedJsonArray.put(jsonValues.get(i));
}
jsonArrStr = sortedJsonArray.toString();
}
The code you are describing will only work on a json that looks something like this:
[
{ "useCaseId" : "4", ... },
{ "useCaseId" : "1", ... },
{ "useCaseId" : "a", ... },
...
]
As you can see, the string begins with a [ character, like the exception demanded.
Since "most" jsons begin with { I'm guessing that your json structure is different, and then you'll be required to adjust your code accordingly. For example, if your json array is embedded in an object like "most" jsons are:
{
"useCases" : [
{ "useCaseId" : "4", ... },
{ "useCaseId" : "1", ... },
{ "useCaseId" : "a", ... },
...
]
}
then you would have to create a JSONObject obj = new JSONObject(jsonArrStr) and then get the JSONArray by calling (JSONArray)obj.get("useCases").
Trying to parse the following JSON return to java usable code:
{
"destination_addresses" : [ "New York, NY, USA" ],
"origin_addresses" : [ "Washington, DC, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "225 mi",
"value" : 361951
},
"duration" : {
"text" : "3 hours 51 mins",
"value" : 13876
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
// Parse the whole JSON result.
JSONObject o1 =(JSONObject)JSONValue.parse(outputString);
JSONArray a1 = (JSONArray) o1.get("rows");//2
for (int i = 0; i < a1.size(); i++) {
JSONObject o2 = (JSONObject) a1.get(i);//3
JSONArray a2 = (JSONArray) o2.get("elements");//4
for (int j = 0; j < a2.size(); j++) {
JSONObject o3 = (JSONObject) a2.get(j);//3
JSONArray a3 = (JSONArray) o3.get("distance");//4
for(int k =0; k < a3.size(); k++) {
System.out.println(((JSONObject) a3.get(k)).get(
"text").toString());//5
}
}
}
I would like to access text value of distance and also text value of duration . I found code to access distance and duration but when I added the third for loop following the example of the old ones I get this expection
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray
At this line:
JSONArray a3 = (JSONArray) o3.get("distance");//4
Any help or suggestions on how do it in other ways? Thanks
Use JSONObject because its not array
JSONObject a3 = (JSONObject) o3.get("distance");
then
String text=a3.getString("text");
String value=a3.getString("value");
With the help of #sasikumar
JSONObject a3 = (JSONObject) o3.get("distance");
String text = null;
text= (String) a3.get("text");
value=(String )a4.get("text");
That solved it for me.
I have a JSONArray contained in another JSONArray in a JSON Object like the following:
{
"protocol": "test",
"query": [
{
"locked": false,
"ids": [
{
"id": 1,
"locked": false
},
{
"id": 1,
"locked": false
}
]
},
{
"locked": false,
"ids": [
{
"id": 1,
"locked": false
},
{
"id": 1,
"locked": false
}
]
}
],
}
here i want to get the ids arrays, i can get the query arrays like that:
private JSONArray getDB_Query(String json) throws JSONException{
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
Login.errorMessage.setText(sys_err);
}
JSONArray query = jsonObj.getJSONArray("query");
return query;
}
Now if I want to get the ids, is that true :
jsonObj = new JSONObject(query.toString()); // query is the JSONArray I retrieved from the above method
for(int i= 0; i < query.length(); i ++){
JSONArray ids_query = jsonObj.getJSONArray("ids");
}
update your last for loop like:
for(int i= 0; i < query.length(); i ++){
JSONArray ids_query = query.getJSONObject(i).getJSONArray("ids");
}