get values of array within array json in java - java

I need to get the values of the array "q1" which in array "questions", the arrays is json, and i need the values in java android.
{
"questions": [
{
"q1": [
"what my name?",
"a",
"b",
"c",
"Mac"
],
"q2": [
"what my age?",
"1",
"34",
"80",
"3"
]
}
]
}

Here I am answering your question to solve your problem but My friend you need to study first about json parsing.
Suggestion :
You can find example of it here :
http://primalpappachan.com/android/2010/06/05/parsing-json-in-android/
Answer :
JSONObject jresonseobj = response.getjObj();
JSONObject jobj;
JSONArray jsonArray = new JSONArray(jresonseobj.getString("questions"));
for (int i = 0; i < jsonArray.length(); i++)
{
jobj = jsonArray.getJSONObject(i);
// parse inner json here.
}

Related

Cucumber:Assert values irrespective of Index

I am working on updating functional test suites using Cucumber feature file.The issue my output is an array which is not sorted.Index of the object may change.
Array:
[{
"id": "12",
"name": "Something"
},
{
"id": "13",
"name": "Another Something"
}
]
Here I wanna assert name when Id=13 only.Any help would be appreciated.
The Json is list of objects within an Array so you need parse them and validate the each Object. You can do like below,
Code:
JSONArray jsonArray = new JSONArray(JsonAsString);
JSONObject jsonObject;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = new JSONObject(jsonArray.get(i).toString());
if (jsonObject.get("id").toString().equalsIgnoreCase("13")) {
System.out.println("Name: " + jsonObject.get("name"));
//do your thing...
}
}
Output:
Name: Another Something

Java: Array of Arrays from JSON

I have a JSON Arrays of Array like this
"results": [
{
"id": "AAA",
"color": "#4D4837",
"links": {
"self": "https://aaa.com",
"html": "https://bbb.com",
"download": "https://ccc.com",
"download_location": "ddd.com"
},
"categories": [],
"likes": 3891,
},
{
"id": "BBB",
"color": "#4D453",
"links": {
"self": "https://abb.com",
"html": "https://bcc.com",
"download": "https://ccc.com",
"download_location": "ddd.com"
},
"categories": [],
"likes": 3000,
}
]
And I would like to retrieve "https://bbb.com" and "https://bcc.com" of "html", but I don't know how to do that.
Based on kindly comment, I put the following.
somehow, "getJSONObject()"can not be put. The error message says "Cannot resolve method 'getJSONObject' in 'JSONArray'".
JSONArray array = new JSONArray((Collection) jobjt.get("Strings"));
for (int i =0 ; i<2 ; i++){
JSONObject job = (JSONObject) array.get(i); --> get(i) can not be changed to getJSONObject(i)
String id = job.get("id").toString();
String color = job.get("color").toString();
String photoUrl = job.get("links").toString(); --> By updating here, I want to store only "https://bbb.com" and "https://bcc.com".
}
But when I tried to use the following, not only "html", but "self" and the other information are retrieved.
String photoUrl = job.get("links").toString();
Please tell me how to retrieve only "html".
I am using IntelliJ.
Steps to be followed(assuming you have proper JSONArray you mentioned):
get JSONObject from your JSONArray by index ie. for your case, index=0 here
Get the inner JSONObject by key of links
Now, access your content by key of html
Example for your case:
JSONObject indexedObject = jsonArray.getJSONObject(0);
JSONObject linksObject = indexedObject.getJSONObject("links");
String html= linksObject.getString("html");
Better keep checking if key exists as Harshal suggests

How to parse this json in android studio?

{
"result_count": 251175,
"images": [
{
"id": "649682800",
"display_sizes": [
{
"is_watermarked": false,
"name": "thumb",
"uri": "https://media.gettyimages.com/photos/sergi-roberto-of-barcelona-celebrates-as-he-scores-their-sixth-goal-picture-id649682800?b=1&k=6&m=649682800&s=170x170&h=UjuhQ2k4pOnhCh5a1oLuC4t5rwX8332a-PEqZ8dpUfY="
}
],
"referral_destinations": [
{
"site_name": "gettyimages",
"uri": "https://www.gettyimages.com/detail/news-photo/sergi-roberto-of-barcelona-celebrates-as-he-scores-their-news-photo/649682800"
}
],
"title": "FC Barcelona v Paris Saint-Germain - UEFA Champions League Round of 16: Second Leg"
}
The above JSON Data is missing ] } . So make the data a valid JSON by adding ] } at the end
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.optJSONArray("images");
for(int i=0; i< jsonArray.length(); i++)
{
JSONObject jsonObjec = jsonArray.optJSONObject(i);
jsonObjec.optString("id");
jsonObjec.optString("title");
JSONArray display_sizes = jsonObjec.optJSONArray("display_sizes");
// get the display_sizes Array details
JSONArray referral_destinations = jsonObjec.optJSONArray("referral_destinations");
// get the referral_destinations Array details
}
First it is invalid json.
Try using gson library to parse the json.
Try this link
gson library to parse json

Return Java JsonPath as a json string

Using the java json path library: https://github.com/json-path/JsonPath
Given a json like this
{
"store": {
"book": [
{
"category": "reference"
},
{
"category": "fiction"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
I would like to return "book" as a list of strings. For examples:
List<String> results = JsonPath.read(example, "$.store.book[*]");
And results should look like:
["{\"category\":\"reference\"}", "{\"category\":\"fiction\"}"]
Is there a way to achieve this?
Currently:
System.out.println(udf.jsonExtractScalar(testExample, "$.store.book[*]").getClass().getName());
--> net.minidev.json.JSONArray
As book is a JsonArray in you json tree, you can use JsonArray type and retrieve the data from it and add to the list.
Here is an example.
JSONArray jsonArray = new JSONArray(jsonArrayString);
List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
list.add( jsonArray.getString(i) );
Replace jsonArrayString with you jsondata's books array name and you will be good to go.
The class used will be JSONArray class which is
`org.json.JSONArray`

Parsing JSON array if I don't know array names

I've got this JSON file. But what to do if I dont know array names (First, Second). How to parse this file in android?
Thanx
{
"First": [
"Test1",
"Test2",
"Test3",
"Test4",
"Test5"
],
"Second": [
"Test1",
"Test2"
] }
JSON you're working with already is an array. So, don't construct an instance of JSONObject; use a JSONArray. This should suffice:
JSONArray jsonArray = new JSONArray(result);
for(int i=0;i<jsonArray.length();i++){
JSONObject e = jsonArray.getJSONObject(i);
String ex = jsonArray.getString("")
}

Categories

Resources