Query a JSONObject in java
I've just unexpectedly found very interesting project: JSON Path
JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document.
With this library you can do what you are requesting even easier, then my previous suggestion:
String hello = JsonPath.read(json, "$.data.data2.value");
System.out.println(hello); //prints hello
Hope this might be helpful either.
Querying JSON Object in Java
For example, use library "Josson & Jossons"
https://github.com/octomix/josson
implementation 'com.octomix.josson:josson:1.3.22'
------------------------------------------------
Josson josson = Josson.fromJsonString(
"{" +
" \"Name\" : \"Anmol Jain\"," +
" \"Address\" : [" +
" {" +
" \"type\" : \"home\"," +
" \"category\" : \"primary\"," +
" \"street\" : \"ABC\"" +
" }," +
" {" +
" \"type\" : \"home\"," +
" \"category\" : \"secondary\"," +
" \"street\" : \"XYZ\"" +
" }," +
" {" +
" \"type\" : \"work\"," +
" \"category\" : \"primary\"," +
" \"street\" : \"PQR\"" +
" }" +
" ]" +
"}");
String street = josson.getString("Address[type='home' & category='secondary'].street");
How to query on parse json object child value if it contains/equal to/greater than etc.?
You can try this code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
query.whereGreaterThan("empInfo.salary", 1000);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objs, ParseException e) {
}
});
Query a JSON Object in Java by index
I think that you need to go down another level of array to access the value you required.
JsonArray data = dataset.getJsonArray("data");
JsonArray firstPieceOfData = data.get(0);
Object firstRate = firstPieceOfData.get(1);
How to search/find In JSON with java
You can also use the JsonPath project provided by REST Assured. This JsonPath project uses Groovy GPath expressions. In Maven you can depend on it like this:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
Examples:
To get a list of all book categories:
List<String> categories = JsonPath.from(json).get("store.book.category");
Get the first book category:
String category = JsonPath.from(json).get("store.book[0].category");
Get the last book category:
String category = JsonPath.from(json).get("store.book[-1].category");
Get all books with price between 5 and 15:
List<Map> books = JsonPath.from(json).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");
GPath is very powerful and you can make use of higher order functions and all Groovy data structures in your path expressions.
Get in to JSON all the rows from sql query in JAVA
You are iterating over resultset and adding each row into JSONObject
but not adding JSONObject
into the JSONArray
.
Just add JSONObject (obj)
into JSONArray (json)
and you will get all rows/records in the response. :)
functions json object, json array in query builder using JOOQ
It doesn't look like you've correctly configured jOOQ to use SQLDialect.POSTGRES
in your defaultDSLContext
instance, otherwise it wouldn't generate standard SQL/JSON syntax like:
json_arrayagg(json_object(key ? value ...))
Related Topics
How to Add New Methods to the String Class in Java
How to Use Regular Expressions in Web.Xml Url Patterns
Java String Array: Is There a Size of Method
Number of Decimal Digits in a Double
Displaying Am and Pm in Lower Case After Date Formatting
Create Simple Pojo Classes (Bytecode) at Runtime (Dynamically)
Getting Class Cast Exception Where Both Classes Are Exactly the Same
Differencebetween a += B and a =+ B , Also A++ and ++A
Java String Concatenation with + Operator
Changing Java Platform on Which Netbeans Runs
Rethrowing an Exception: Why Does the Method Compile Without a Throws Clause
Are Thread.Sleep(0) and Thread.Yield() Statements Equivalent
Java Byte Array Contains Negative Numbers
Post Increment Operator Not Incrementing in for Loop