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")
Related
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.
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")
I'm exporting some data in java using JSON then I'm reading that data and trying to get elements from an array inside the JSON object but I'm having issues.
I have tried a lot of things like
jsonObject.get("InGameCord").get("x")
Object Testo = jsonObject.get("InGameCord");
Testo.x
Things like that along with more that did not work so deleted the code.
This is the exported JSON file and im trying to access the InGameCord array X or Y.
{"BaseID":1,"BaseName":"Bandar-e-Jask Airbase","InGameCord":[{"x":463,"y":451}]}
Here is my file reader code
FileReader reader = new FileReader(filename);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
System.out.println(jsonObject);
System.out.println("BaseName: "+jsonObject.get("BaseName"));
System.out.println("BaseID: "+jsonObject.get("BaseID"));
System.out.println("InGameCord: "+jsonObject.get("InGameCord"));
All of this works and exports the correct info.
So I'm trying to get let us say the X value of InGameCord.
int X = 463;
Given your JSON data {"BaseID":1,"BaseName":"Bandar-e-Jask Airbase","InGameCord":[{"x":463,"y":451}]}:
"InGameCord" is the name of an array which can be instantiated as a JSONArray.
That array contains only one element: {"x":463,"y":451}.
That array element can be instantiated as a JSONObject. It contains two name/value pairs:
"x" with the value 463.
"y" with the value 451.
So based on the code you provided, to instantiate the JSONArray:
JSONArray numbers = (JSONArray) jsonObject.get("InGameCord");
To retrieve the first (and only) element of the array into a JSONObject:
JSONObject jObj = (JSONObject) numbers.get(0);
To get the value for "x" into an int variable cast the Object returned by get() to a Number, and then get its intValue():
int value = ((Number) jObj.get("x")).intValue();
You can even do the whole thing in one line, but it's ugly:
int y = ((Number) ((JSONObject) numbers.get(0)).get("y")).intValue();
I have an array of strings like [1234_acb,2345_xyz]
I want to form a key value pair JSON Object like [{"1234":"abc"},{"2345":"xyz"}]
I have used split function to separate out the values from underscore
assume your string data is stored in data:
String keyValArray[] = data.split("_");
JSONArray jsonArray = new JSONArray();
for(int a=0;a<keyValArray.length-1;a+=2){
jsonArray.put(new JSONObject().put(keyValArray[a], keyValArray[a+1]));
}//for loop
String jsonStr = jsonArray.toString();
P.S: for loop limit is keyValArray.length-1 so keyValArray[a+1] does not go out of the array bound
P.S2: #njzk2 a++ changed to a+=2, to skip the value item in array, so it's not used as a key in the json object
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);