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")
Related
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);```
This question already has answers here:
parsing json with java
(5 answers)
Closed 5 years ago.
I need to create json string as per below using java: under ADDTNLINFO element how to add multiple elements as Tag .could any one help please ?
{
"ADDTNLINFO":
{
"Tag":
{
"name": "a",
"value": "10"
},
"Tag":
{
"name": "a b",
"value": "20"
},
"Tag":
{
"name": "a b c",
"value": "30"
}
}
}
import com.google.gson.Gson;
List<Dto> beans = service.something();
Gson gson = new Gson();
// convert your list to json
String jsonCartList = gson.toJson(beans);
You need to add Dependency for Google gson and it will according to your requirement
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I need the count the number of specific array (coreid) present in response JSON Since, response I'm getting is pretty huge, I am using following JSON as example:
{
"errors": [],
"data": {
"products": {
"number": 0,
"size": 2,
"total": 2,
"results": [{
"assetId": "29-test-ENG",
"fields": {
"majoracademic": [
"test stream 1"
],
"learningobjective": [
"test objective 1"
],
"hasproductprice": [
"0"
],
"comingsoon": [
"0"
],
"coreid": [
"12"
],
"previewCount": [
"0"
],
"type": [
"page(s)"
]
}
},
{
"assetId": "01-test-MTH",
"fields": {
"majoracademic": [
"test stream 2"
],
"learningobjective": [
"test objective 2"
],
"hasproductprice": [
"0"
],
"comingsoon": [
"0"
],
"coreid": [
"12"
],
"previewCount": [
"0"
],
"type": [
"page(s)"
]
}
}
]
}
}
}
So Basically I need to count array "coreid" occurrence in this JSON, which is actually 2 but my code is returning 3 times, I'm using the following code:
protected String getTokenValueUnderHierarchyString( String Json) throws ClientProtocolException, IOException {
List<String> list = Arrays.asList(Json.split("coreid"));
System.out.println("list.size()::"+list.size());
}
I printed the values of array, turns out it takes complete JSON object as 1st element.
So even If I type "coreid1, array count it will return is 1. and the value would be complete JSON.
I hope my question is making any sense.
Split is not meant to count occurences. It split (hence the name ;) ) a string at a given separator. You have three because you have some text before the first coreid some text between the two and some text after the last.
Also, your solution will break if the string coreid is present anywhere else. A better approach would be to parse the JSON and then analyse it to find the number of key that are coreid.
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
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");