How get value from JsonArray? [duplicate] - java

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 2 years ago.
I'm just getting started with using json with java. I'm not sure how to access string values within a JSONArray. For instance, my json looks like this:
"media": {
"images": [
"https://newstaging-api.safegold.com"
],
"videos": [
"https://newstaging-api.safegold.com"
]
}
Now I want to get the value of "images".Actually I want show image in ImageView from URL.
Thanks in advance

JSONObject json = new JSONObject(json_data); //json_data - your json string
JSONObject media = json.getJSONObject("media");
JSONArray images = media.getJSONArray("images");
String link = images.getString(0);```

Related

Complicated Json parsing in java [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 4 years ago.
i want to parse this code to get the id's location in an array.
Json is as follows:
"tipSecurableSet": {
"id": "082bdd09-6cff-41f9-a6f6-16a5bcc2eaa6",
"items": [
{
"typeId": "c5831702-15ed-483d-8253-c890e3f97dae",
"ids": [
"44b3efd4-5f7f-4698-aba4-1f4d9605c4eb"
],
"id": "b838aa0d-522a-411e-958a-41c711c6a856"
},
{
"typeId": "01c3c7de-2856-4665-90bc-a614caf335cd",
"ids": [
"8240a190-7e14-4ba4-87be-22febd09fa69"
],
"id": "62e62bf5-7a18-4518-a917-bb908d61fdd2"
}
]
}
}
You should take a look at Org.JSON library.
You can get the IDs very easily once you have your JSONObject. There are many ways to construct a JSONObject. You can parse XML.toJSONObject or use one of the JSONObject constructors.
JSONObject obj= new JSONObject(...);
Iterator<String> keys= obj.keys(); //Gets your Fields or Keys
To get the object for a key, here is an example with a String and an Array of JSONObjects.
String id= jsonArray.getJSONObject(i).getString("typeID")

gson parsing JSON object to map from a complex JSON file [duplicate]

This question already has answers here:
How can I convert JSON to a HashMap using Gson?
(16 answers)
Closed 5 years ago.
I have a problem trying to parse a json file to JSON object
My JSON File is under the form :
{ "objectJSON": [
{
"attribute":"value",
"attribute":"value",
"attribute":"value",
"attributes" :{
"att1": "val1",
"att2":"val2",
"att3":"val3",
// more atts and vals may figure here !
}
},
{
"attribute":"value",
"attribute":"value",
"attribute":"value",
"attributes" :{
"att1": "val1",
"att2":"val2",
"att3":"val3",
// more atts and vals may figure here !
}
}
]
}
my problem is that the object "attributes" has an unknown number of arguments so I can not create a class for it , I thought about using a
map<String,String> attributes;
but I don't know how to parse it from the file especially that I want to keep my class "ObjectJSON" witch represents the root of my file.
I'm using gson of google
thanks in advance .
Using org.json library:
JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");

Android Json parsing with multiple JsonArray's [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 7 years ago.
I am trying to parse JSON data which is like:
{
"name1": "xyz",
"data":[{
"education": {
"School": "xyz",
"ug": "xyz",
"Activities": [{
...
}],
"Prizes": [{
...
}],
"Curriculum":[{
...
}]
}
}]
}
How can I fetch the JSONArray of activities, prizes, curriculum values?
If you just want the values of the first element (you are looking at an array so you may want all the elements) you can try this:
String src = " { ... } "; //your json
JSONObject mainObject= new JSONObject(src);
JSONArray dataArray= mainObject.getJSONArray("data");
JSONObject firstDataObject = dataArray.getJSONObject(0); //get the first element
JSONObject educationObject = firstDataObject.getJSONObject("education");
JSONArray activitiesArray = educationObject.getJSONArray("Activities");
//do something with the array. Ex: activitiesArray.getJSONObject(0);
JSONArray prizesArray = educationObject.getJSONArray("Prizes");
//do something with the array
JSONArray curriculumArray = educationObject.getJSONArray("Curriculum");
//do something with the array

How do I parse a JSON array in Java? [duplicate]

This question already has answers here:
Parsing JSON Object in Java [duplicate]
(5 answers)
Closed 7 years ago.
I have the following JSON text and would like to get the values "detest" and "dislike".
{
"noun": {
"syn": [
"hatred",
"emotion"
],
"ant": [
"love"
]
},
"verb": {
"syn": [
"detest",
"dislike"
],
"ant": [
"love"
]
}
}
I've tried parsing it using the org.json library with the following:
JSONArray obj = new JSONObject(String);
JSONArray arr = obj.getJSONArray("syn");
But I can't seem to access the array this way.
Any help is greatly appreciated.
The following is related to the JSON.simple library.
You need a JSONParser instance to get a JSONObject value from your String object.
JSONObject data;
try {
JSONParser helper = new JSONParser();
data = (JSONObject)helper.parse(String);
} catch (ParseException parse) {
// Invalid syntax
return;
}
// Note that these may throw several exceptions
JSONObject node = (JSONObject)data.get("verb");
JSONArray array = (JSONArray)node.get("syn");

How to extract a specific link in a json file [duplicate]

This question already has answers here:
How to parse JSON file?
(3 answers)
Closed 8 years ago.
"papers": [
{
"files": [
{
"url": "http://farnsworth.papro.org.uk/file/977",
"type": "Initial XML version of the paper",
"name": "c6938201-dac0-4ef9-91cd-ca6e8c30f4b8.xml"
},
{
"url": "http://farnsworth.papro.org.uk/file/978",
"type": "Final annotated XML file",
"name": "c6938201-dac0-4ef9-91cd-ca6e8c30f4b8_final.xml"
}
]
How can i get the first url in this json file ? What I have so far is:
JSONObject file = (JSONObject) files.get(j);
String url = (String) file.get("url");
System.out.println(url);
something like this should work :
JSONArray results = d.getJSONArray("files");
JSONObject p = (JSONObject)results.get(0);
url = p.getString("url");

Categories

Resources