Je souhaite convertir le JSON suivant en utilisant JsonObect et JsonArray mais je ne parviens pas à le faire.
{
"query": {
"bool": {
"must": [
{
"match": {
"customer.partnerName": "Synapse"
}
},
{
"range": {
"customer.billing.chargeAmount": {
"gte": 1,
"lte": 100
}
}
}
],
"filter": [
{
"match": {
"customer.configId": 15
}
}
]
}
}
}
J'ai essayé d'utiliser JsonObject code > mais pas en mesure d'obtenir le résultat.
6 Réponses :
Je pense que ce que vous recherchez, c'est l'analyse json. Cela se fait de la manière suivante:
JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(jsonData); //Insert json string data //Do other stuff
<script>
var txt = '{"query": {"bool": {"must": [{"match": { "customer.partnerName": "Synapse" }},{"range" : { "customer.billing.chargeAmount" : { "gte" : 1, "lte" : 100 } }}],"filter": [{ "match": { "customer.configId": 15 }}]}}}'
var obj = JSON.parse(txt);
debugger;
document.getElementById("demo").innerHTML = obj.query;
</script>
Et pourquoi pensez-vous que c'est une question js?
Avez-vous essayé Google gson? Voici le repo, vous pouvez également trouver des implémentations pertinentes en ligne. https://github.com/google/gson
Essayez ceci: -
JSONObject jsonObject = new JSONObject(/*Pass your string value here*/ new JSONTokener(result.toString()).nextValue().toString());
//get 'query' as JSONObject
JSONObject jresponseData = new JSONObject(jsonObject.getString("query"));
//since 'bool' is insode 'query'
JSONObject jresponseData_2 =jresponseData.getString("bool");
JSONArray jsonArray = new JSONArray(jresponseData_2.getString("must"));
Et vous obtiendrez le résultat dans JSONArray
Donc, je dirais que vous devriez utiliser JsonPath lib pour faire cela.
{
"feed": {
"data": [
{
"created_time": "2017-12-12T01:24:21+0000",
"message": "This picture of my grandson with Santa",
"id": ""
},
{
"created_time": "",
"message": "",
"id": ""
},
{
"created_time": "",
"message": "",
"id": ""
}
],
"paging": {
"previous": "https://facebook/v3.2/{your-user-id}/feed?format=json&limit=3&since=1542820440",
"next": "https://facebook/v3.2/{your-user-id}/feed?format=json&limit=3&until=1542583212&"
}
},
"id": "{your-user-id}"
}
Exemple d'utilisation
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
...
public void handle(...) {
...
DocumentContext jsonContext = JsonPath.parse(responseBody);
JSONArray jsonPathPreviousUrl = jsonContext.read("$..previous")
...
Cela analysera votre json rapidement, efficacement.
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
Ceci est juste un simple copier / coller de votre chaîne json dans AndroidStudio, il divise la chaîne automatiquement et ajoute des barres obliques d'échappement .. cela a l'air horrible mais la syntaxe que vous avez écrite est parfaitement correcte ..
try {
JSONObject jsonObject = new JSONObject();
JSONObject query = new JSONObject();
jsonObject.put("query", query);
JSONObject bool = new JSONObject();
query.put("bool", bool);
JSONArray must = new JSONArray();
bool.put("must", must);
JSONObject matchWrap = new JSONObject();
JSONObject match = new JSONObject();
match.put("customer.partnerName", "Synapse");
matchWrap.put("match", match);
must.put(matchWrap);
JSONObject rangeWrap = new JSONObject();
JSONObject range = new JSONObject();
JSONObject customerBillingChargeAmount = new JSONObject();
customerBillingChargeAmount.put("gte", 1);
customerBillingChargeAmount.put("lte", 100);
range.put("customer.billing.chargeAmount", customerBillingChargeAmount);
rangeWrap.put("range", range);
must.put(rangeWrap);
JSONArray filter = new JSONArray();
bool.put("filter", filter);
JSONObject match2Wrap = new JSONObject();
JSONObject match2 = new JSONObject();
match2.put("customer.configId", 15);
match2Wrap.put("match", match2);
filter.put(match2Wrap);
String jsonString2 = jsonObject.toString();
// HERE THE SAME JSON STRING AS YOUR INPUT
} catch (JSONException e) {
e.printStackTrace();
}
la deuxième option que vous avez est de créer l'objet et les objets internes et les tableaux par programme .. comme ceci ..
String jsonString = " {\n" +
" \"query\": {\n" +
" \"bool\": {\n" +
" \"must\": [\n" +
" {\"match\": \n" +
" { \"customer.partnerName\": \"Synapse\" }},\n" +
"\n" +
" {\n" +
"\"range\" : \n" +
"{\n" +
" \"customer.billing.chargeAmount\" : {\n" +
" \"gte\" : 1,\n" +
" \"lte\" : 100\n" +
" }\n" +
" }}\n" +
" ],\n" +
" \"filter\": [\n" +
" { \"match\": { \"customer.configId\": 15 }}\n" +
" ]\n" +
" }\n" +
" }\n" +
" }";
// HERE BEAUTIFIED
/*jsonString = "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"customer.partnerName\":\"Synapse\"}},{\"range\":{\"customer.billing.chargeAmount\":{\"gte\":1,\"lte\":100}}}],\"filter\":[{\"match\":{\"customer.configId\":15}}]}}}";
*/
try {
JSONObject object = new JSONObject(jsonString);
// NO ERRORS, OBJECT CREATED IN MY CASE
} catch (JSONException e) {
e.printStackTrace();
}
Cela donne le même rasult que votre chaîne d'entrée lorsqu'elle est supprimée de sauts de ligne de tabulations d'espaces blancs etc.
C'est une requête elasticsearch, que voulez-vous faire exactement avec cela?