How to parse a single array, without a name associated with it? - java

I have the following JSON:
[{
"aaa": "blah",
"ddd": 2
}]
Note that the map is inside an array. How to get the map and then the value of "aaa".
Using Json Simple.
Thanks!

The following code should work. Let me know if it doesn't!
Object obj = JSONValue.parse(jsonString);
JSONArray array = (JSONArray)obj;
JSONObject obj2 = (JSONObject)array.get(0);
String result = obj2.get("aaa")

Related

How to put data to parsed JSONObject

i have some parse code and to parsed JSONObject i need to add one more JSONObject, but getting error Unexpected token LEFT BRACE({), because my code creating multiply JSONObjects in file, not at parsed JSONObjec. Here is a code, that creating object
aJson = (JSONObject) parser.parse(reader);
JSONObject json = new JSONObject();
JSONArray blockData = new JSONArray();
for(Block b : blocks){
json.put("player-name", p.getName());
json.put("uuid", p.getUniqueId().toString());
json.put("nearestPlayers", new JSONArray());
blockData.add(b.getLocation().getWorld().getName());
blockData.add(b.getLocation().getWorld().getEnvironment());
blockData.add(b.getLocation().getX());
blockData.add(b.getLocation().getY());
blockData.add(b.getLocation().getZ());
}
aJson.put(blockData, json);
Here is JSON
{"[\"world\",NORMAL,-23.0,67.0,75.0]":{"player-name":"MisterFunny01","nearestPlayers":[],"uuid":"206d32da-bf72-3cfd-9a26-e374dd76da31"}} //here is that part// {"[\"world\",NORMAL,-23.0,67.0,75.0]":{"player-name":"MisterFunny01","nearestPlayers":[],"uuid":"206d32da-bf72-3cfd-9a26-e374dd76da31"},"[\"world\",NORMAL,-23.0,67.0,75.0]":{"player-name":"MisterFunny01","nearestPlayers":[],"uuid":"206d32da-bf72-3cfd-9a26-e374dd76da31"}}
In JSON array values must be of type string, number, object, array, boolean or null. Arrays hold values of the same type and not different types.
Looking at your code the array is an array of objects. So you would have to create an object and add the values before adding to the array.
Don't directly add values to the array but create an object and then add to the array.
Your code is wrong. To put an object into JSONObject please read this document
In your case, you need to convert blockData to String to put in the JSONObject.
It's like this: aJson.put(blockData as String, json);
Hope it can be helpful to you.

Verify List of Json Object empty

How to check a List is empty or not when it contains empty Json object.
Below is the example:
"data": {"parseList" : [{}]}
now I want to check parseList is empty or not , I tried using
collectionUtils.isEmpty(parseList);
but it is not working any help is appreciated.
You can use length() method of JSONObject which returns the count of key-value pairs inside a JSONObject.
So you can find out if your object is empty or not.
You can parse Json using org.json and obtain JsonArray object. The length() method could be used to find the number of elements inside JsonArray.
String stringToParse = "";
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
JSONArray array = myjson.getJSONArray("parseList");
System.out.println(array.length());

JSON Deserialization - JSON Array to ArrayList of JSON Objects

I am trying to deserialize a string which is a JSON Array in Java using fasterxml. And I want to convert this into an ArrayList of JSONObjects.
[
{
"test": "hello"
},
{
"anotherTest": "world"
}
]
When I try to use the object mapper as follows,
ArrayList<JSONObject> list = mapper.readValue(sourceString, ArrayList.class);
I am getting an ArrayList which contains LinkedHashMap.
I tried to change my type using the below code.
ArrayList<JSONObject> list = mapper.readValue(s, mapper.getTypeFactory().constructCollectionType(List.class, JSONObject.class));
And even this...
ArrayList<JSONObject> list = mapper.readValue(s,new TypeReference<List<JSONObject>>() {});
Both it did not help me.
Any thoughts?

Passing json array in a key value pair

I want to know how to pass the json array as key in a json object.
{
"name" :"Sam",
"grades": [{"maths": "A", "result":"pass"}, {"science": "B", "result":"pass"}]
}
I couldn't pass both the values to 'grades' in jSONObject. I Looped it. But, it simply overwrites the values.
It seems like you are doing something like:
obj.put("grades", mathGrade);
obj.put("grades", scienceGrade);
Where the scienceGrade just overwrites mathGrade.
What you should be doing is using an intermediate array object:
JSONArray grades = new JSONArray();
grades.put(mathGrade);
grades.put(scienceGrade);
obj.put("grades", grades);

parse json data already in an array?

I have the following data:
[{"class":"test","description":"o hai","example":"a","banana":"b"}]
As this JSON data is already in an array, I'm having troubles to parse this with JSON simple:
File file = new File( "/Users/FLX/test.json");
String s = FileUtils.readFileToString(file);
Object obj = parser.parse(s);
JSONArray array = (JSONArray) obj;
log.warn("WAAAAT"+array.get(1));
This doesn't work because "1" (description) is in array 0, which causes an out of bounds exception, how can I properly do this?
[] denotes an array, while {} denotes an object, so you have an array of objects.
The way your JSON is formatted, you have an array which contains a single object. That single object has properties named "class", "description", "example", and "banana" with values "test", "o hai", "a", and "b" respectively.
JSONArray is 0 based so array.get(1) would be out of bounds. In order to get description you would do something like array.getJSONObject(0).get("description")

Categories

Resources