如何有效地解析每次演变的Rest Api响应 [英] How to parse efficiently a Rest Api response that evolve each time

查看:130
本文介绍了如何有效地解析每次演变的Rest Api响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是程序员,所以我掌握了基础知识.

I am not a programmer, then I lake basic knowledge.

我想创建一个桌面应用程序,字典,搜索单词,定义简短.

I want to create a desktop app, a dictionary, you search a word, you have a short definition.

我用那些家伙.

https://api.lexicala.com/

然后这是我的脚本的一部分.

Then here is part of my script bellow.

我的问题是,对于我搜索的每个部分,单词的含义,其性质等.

My problem is that for each part I search for, the meaning of the word, its nature, etc.

我必须制作此解封装代码行. //decapsulation =反序列化

I have to make this decapsulation code lines. // decapsulation=deserialization

response.getBody().getObject().getJSONArray("results").getJSONObject(0).getJSONObject("headword").get("text")

如果Json每次都没有改变,那没问题,但是确实有.

If the Json was not changing each time, no problem, but it does.

一个形容词和一个名词完全改变了结构,我得到一个错误.

An Adjective, and a noun change completely the structure, and I get an error.

Json对象现在是Json数组.

A Json Object is now a Json Array.

我想知道是否有一种简单的方法,无论是杰克逊(Jackson)还是格森(Gson)或杰森(Json) 只是问类似的东西

I would like to know if there is a simple way, either Jackson, or Gson or Json to simply ask something like

Json.Parse (JsonResponse);
get("Definition");

代替

Object Resultat4 = response.getBody().getObject().getJSONArray("results").getJSONObject(0).getJSONArray("senses").getJSONObject(0).get("definition");

仅包含内容,而无需绕过所有这些不断变化的代码行.

And have the content only, without passing by all this changing lines of codes.

谢谢您的帮助.

//Values

String motacherch = "grand";



public void  gettest(){



   HttpResponse<JsonNode> response = Unirest.get("https://dictapi.lexicala.com/search?source=global&language=fr&text="+motacherch)
           .basicAuth("user", "password")
           .asJson();

   JSONObject responsejson = (JSONObject) response.getBody().getObject();
   System.out.println( responsejson);


   Object Resultat = response.getBody().getObject().get("n_results");
   System.out.println( "Le nombre de resultats est:"+ Resultat);


    Object Resultat2 = response.getBody().getObject().getJSONArray("results").getJSONObject(0).getJSONObject("headword").get("text");
    System.out.println( "Le mot que l'on cherche est: "+ Resultat2);
    Object Resultat3 = response.getBody().getObject().getJSONArray("results").getJSONObject(0).getJSONObject("headword").get("pos");
    System.out.println( "C'est un: "+ Resultat3);
    Object Resultat4 = response.getBody().getObject().getJSONArray("results").getJSONObject(0).getJSONArray("senses").getJSONObject(0).get("definition");

    System.out.println( "Definition: "+ Resultat4);


    System.out.println( "---------------------------------------------" );

HttpResponse<JsonNode> response2 = Unirest.get("https://dictapi.lexicala.com/users/me")
        .basicAuth("", "")
        .asJson();
    //System.out.println( response2.getBody());
    JSONObject responsejson2 = response2.getBody().getObject();
    JSONObject results = responsejson2.getJSONObject("usage").getJSONObject("today");
    Object results2=results.get("count");
    System.out.println( results2);

}

推荐答案

最简单的方法是将json解析(反序列化)为Java Map<String, Object>,它会使用您喜欢的库始终成功(值本身可能是maps) (尝试Jackson或gson).

The simplest way is to parse (deserialise) the json to a java Map<String, Object>, which always succeeds (the values may themselves be maps) using your favourite library (try Jackson or gson).

然后检查地图的键以弄清楚该怎么做.

Then examine the map’s keys to figure out what to do with it.

更困难但更正确"的方法是反序列化由有效负载的属性选择的各种类,但是在这种情况下我不会这样做,因为API设计听起来有些胡扯.

The harder, but more "correct" way is to deserialise to a variety of classes chosen by the attributes of the payload, but in this case I wouldn’t, as the API design sounds a little flakey.

以下是一些将json解析为地图的代码:

Here’s some code to parse json to a map:

import com.fasterxml.jackson.databind.ObjectMapper;

Map<String, Object> map = (HashMap<String, Object>) new ObjectMapper().readValue(jsonStr, LinkedHashMap.class); // LinkedHashMap will preserve order

要使访问地图值更容易,您可能会发现此帮助程序方法可以方便地进行推断转换:

To make accessing map values easier, you may find this helper method that does the cast by inference handy:

static <T> T getValue(Map<String, Object> map, String key) {
    return (T)map.get(key);
}

像这样使用它:

List<Map<String, Object>> list = getValue(map, "results"); // no cast needed

这篇关于如何有效地解析每次演变的Rest Api响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆