This is my JSON file
{
"content": [
{
"a":{
"b" : "abcd",
"c" : "bcd"
}
"ab" : "123",
"abc":{
"id" : "12345",
"name" : "abcde"
}
"cd": "afsf"
},
{
"a":{
"b" : "abcd",
"c" : "bcd"
}
"ab" : "123",
"abc":{
"id" : "12345",
"name" : "abcde"
}
"cd": "afsf"
}
]
}
I want the "id" of object "abc" in Java?
I created a object of content and the code is below
JSONArray content = (JSONArray) jsonObject.get("content");
for (int i = 0; i < content.length(); i++) {
JSONObject inner = (JSONObject) content.getJSONObject(i);
String abc = inner.getString("Loan_Application__r");
}
How to i access now the "id" and "name" in java?
pls ignore if any syntax mistake is their...
Take the below code as a hint to approach your problem:-
public static void main(String[] args){
String json="{ \"content\": [ {\"a\":{ \"b\" : \"abcd\", \"c\" : \"bcd\"},\"ab\" : \"123\",\"abc\":{\"id\" : \"12345\", \"name\" : \"abcde\"},\"cd\": \"afsf\"},{\"a\":{\"b\" : \"abcd\", \"c\" : \"bcd\"},\"ab\" : \"123\",\"abc\":{\"id\" : \"12346\",\"name\" : \"abcde\"},\"cd\": \"afsf\"}]}";
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("content");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objects = jsonArray.getJSONObject(i);
String[] elementNames = JSONObject.getNames(objects);
for (String elementName : elementNames)
{
if(elementName.equalsIgnoreCase("abc")){
JSONObject value = objects.getJSONObject(elementName);
String[] elementList = JSONObject.getNames(value);
for(String j:elementList){
if(j.equalsIgnoreCase("id")){
System.out.println(value.getString("id"));
}
}
}
}
}
}
Output:-
12345
12346
Related
I'm using volley to make a request to the server, the server respond with a multidimensional array,
and i'm trying to read the "second" array "details" that are inside one show;
This is what im using to read the response:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Shows");
Log.e(TAG, "Response Array: " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
} catch (JSONException e) {
e.printStackTrace();
}
}
I tryed doing whit inside the loop, but it did'n work :/
JSONArray arr = new JSONArray(jsonArray);
for (int e = 0; e < arr.length(); e++) {
Log.e(TAG, "INSIDE");
}
"Shows" : [
{
"details" : [
"id" : 23adda,
"date" : "Monday",
"time" : "5:00PM"
"details: [
"Address" : "123 street";
"City" : "Test"
]
],
"id" : 15sdsd,
"Heading" : "The Big Show",
"Category" : "Family show",
"AssetId" : 8c8be292,
}
{
"details" : [
"id" : 23adda,
"date" : "Monday",
"time" : "5:00PM"
],
"id" : 15sdsd,
"Heading" : "The Big Show",
"Category" : "Family show",
"AssetId" : 8c8be292,
}
]
You can try below code :
JSONArray jsonArray = response.getJSONArray("Shows");//getting array
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject= jsonArray.getJSONObject(i);//getting first element
String id= jsonobject.getString("id");//getting id
String Heading= jsonobject.getString("Heading");//getting Heading
String Category= jsonobject.getString("Category");//getting Category
String AssetId= jsonobject.getString("AssetId");//getting AssetId
System.out.println(""+id+""+Heading+""+AssetId+""+Category) ;
JSONArray jsonObject1= jsonobject.getJSONArray("details"); //getting children array
for (int j = 0; j < jsonObject1.length(); j++) {
JSONObject object1 = jsonObject1.getJSONObject(j);
String id1= object1.getString("id");//getting id
String date= object1.getString("date");//getting date
String time= object1.getString("time");//getting time
System.out.println(""+id1+""+City+""+time) ;
JSONArray jsonObject2= object1.getJSONArray("details"); //getting children array
for (int k = 0; k < jsonObject2.length(); k++) {
JSONObject object2 = jsonObject2.getJSONObject(k);
String Address= object2.getString("Address");//getting Address
String City= object2.getString("City");//getting City
System.out.println(""+Address+""+City) ;
}
}
}
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 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();
}
I am getting the error:
JSONObject["Transaction_details"] not found.
Why is it not able to find Transaction_details?
Code:
for(Iterator it = mapper.readValues(new JsonFactory().createJsonParser(in),JSONObject.class);it.hasNext();)
{
JSONObject js = (JSONObject) it.next();
JSONArray recs = (JSONArray) js.get("Transaction_details");
for (int i = 1; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("merchantid");
String loc = rec.getString("transaction_amount");
}
Snapshot of a part of my schema:
{
"name" : "cica",
"type" : "long"
}, {
"name" : "pica",
"type" : "long"
}, {
"name" : "issuingcountry",
"type" : "string"
}, {
"name" : "Transaction_details",
"type" : {
"type" : "array",
"items" : {
"type" : "record",
"name" : "Transaction_details",
"doc" : "Transaction event details",
"fields" : [ {
"name" : "transactionid",
"type" : "long"
}, {
"name" : "transactiondate",
"type" : "string"
}, {
"name" : "Productcode",
"type" : "string"
}
]
}
Something like the below snippet should work. But, I have not compiled or executed it.
Basically, you would have to check if you are encountering a "Transaction_details" name value. If yes, then you need to retrieve its 'type' array and work with its elements.
Hope this helps!
for(Iterator it = mapper.readValues(new JsonFactory().createJsonParser(in),JSONObject.class);it.hasNext();)
{
JSONObject js = (JSONObject) it.next();
String nameValue = (String) js.get("name");
if ("Transaction_details".equals(nameValue)) {
JSONArray recs = (JSONArray) js.get("type");
for (int i = 1; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
...
}
}
}