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")
This question is scoped to The Java API for JSON Processing (JSR 353).
Given a JsonArray whose elements are JsonString, how can I easily convert this to a List<String> or a Set<String>? For Set<String>, let's assume that the JsonArray contains a unique collection of string values.
Assume I have a JSON object like this:
{
"name" : "James Johns",
"street" : "22 Nancy St.",
"emails" : [
"james#a.com",
"james#b.net",
null,
""
]
}
I want my resulting collection to ignore any null or empty string entries in the emails array.
Let's assume my JsonObject instance has already been created.
JsonObject person = <parse the JSON input>;
JsonArray emails = person.get("emails");
I keep getting stumped on JsonValue and JsonString and trying to get the actual String value. If I use the JsonValue.toString() and JsonString.toString() methods, I get string values that include the double quotes, as if the original JSON was "\"james#a.com\"". I need the string values to be the equivalent JSON value "james#a.com".
Using Java 8, one can easily convert a JsonArray of JsonString to a List like this:
JsonObject person = <parse the JSON input>;
List<String> emailList =
Optional.ofNullable(person.getJsonArray("emails"))
.orElse(JsonValue.EMPTY_JSON_ARRAY) // Use an empty list if the property's value was null
.getValuesAs(JsonString::getString) // Get each JsonString entry as String
.stream() // Turn the collection into a stream for filter/collect
.filter(email -> email!=null && !email.isEmpty()) // Exclude any null/empty entries
.collect(Collectors.toList()); // Create the List
The ofNullable/orElsecombination guarantees I get a JsonArray back, even if that array is empty, thus avoiding any Null Pointer Exception considerations.
The getValuesAs(JsonString::getString) is a JsonArray method that returns a List by applying the JsonString.getString() method to each returned JsonString. The JsonString.getString() method returns the string value "james#a.com" instead of the "\"james#a.com\"" value that the JsonString.toString() method returns.
The stream() method turns the collection into a sequential stream that we can use to filter and collect members of the original collection.
The filter() method applies the Predicate email -> email!=null && !email.isEmpty() to each entry in the stream, throwing out any null entries or entries that are empty strings.
And finally, the collect() method collects the filtered entries into the specified collection type.
To obtain a Set<String>, change the .collect() to use:
.collect(Collectors.toSet())
Once you get accustomed to the Java 8 mechanics, this approach provides a concise, easy to read and understand technique for manipulating collections without the more complicated approach of null/empty checking and using for loops.
Using Jsonobject I extracted data from RawmatrixData and stored it in Object:
org.json.JSONObject item = Fir.getJSONObject(i); Object value1 = item.get("RawMatrixData")`
Now i want to replace data 342771123181 with some string value, how to achieve this?
I tried with ArrayList<String> and ArrayList<ArrayList<String>>.
"LstMatrixFirmInfo": [
{
"RawMatrixData": "[[342771123181,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],[3427714486446,1,2,null,null,null,null,null,null,null,null,null,null,null,28.99,28.99,28.99,25,4.81,4.81,4.81,null,null,null,null,null,null,null,null,null]]]}
Is RawMatrixData supposed be a string like in the image or JSON Array.
If RawMatrixData is supposed to be string then maybe converted to JSONArray
I would just use string replace.
String replacedText = Fir.getString('RawMatrixData').replace('342771123181', 'foobar')
Fir.push('RawMatrixData', replacedText);
The above can be done in one line but for ease of understanding it hasn't. First line gets your string from the Json object then replace the number with foobar.
Then text is pushed back on to the json object overwriting the old value.
The above code i believe sorts out your problem based on the picture you provided.
If the RawMatrixData was supposed to be a JSON array and not a string then in that case you would have to traverse the whole array replace as you go.
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?
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")