如何将任意 JSON 转换为 Java 中可用的结构

编程入门 行业动态 更新时间:2024-10-27 05:26:35
本文介绍了如何将任意 JSON 转换为 Java 中可用的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 gson 将此返回的 JSON 转换为某种数据结构,以便我可以提取有用的数据.

I'm trying to use gson to convert this returned JSON into some kind of data structure such that I can extract useful data.

例如:

http://search.twitter/search.json?q=test&rpp=1

返回:

{
    "completed_in":0.028,
    "max_id":196386333906837504,
    "max_id_str":"196386333906837504",
    "next_page":"?page=2&max_id=196386333906837504&q=test&rpp=1",
    "page":1,
    "query":"test",
    "refresh_url":"?since_id=196386333906837504&q=test",
       "results":[
          {
             "created_at":"Sat, 28 Apr 2012 23:52:05 +0000",
             "from_user":"della_ky",
             "from_user_id":525641596,
             "from_user_id_str":"525641596",
             "from_user_name":"kydella modeste",
             "geo":null,
             "id":196386333906837504,
             "id_str":"196386333906837504",
             "iso_language_code":"en",
             "metadata":{
                "result_type":"recent"
             },
             "profile_image_url":"http://a0.twimg/profile_images/2159990525/webcam-toy-photo3_20_2__normal.jpg",
             "profile_image_url_https":"https://si0.twimg/profile_images/2159990525/webcam-toy-photo3_20_2__normal.jpg",
             "source":"<a href="http://mobile.twitter" rel="nofollow">Mobile Web</a>",
             "text":"RT @Y__U__NOOO: #SongsIKnowOffByHeart ALL SONGS I LISTEN TO. BRAIN, Y U NO REMEMBER TEST ANSWERS LIKE THAT?!?",
             "to_user":null,
             "to_user_id":null,
             "to_user_id_str":null,
             "to_user_name":null
          }
       ],
       "results_per_page":1,
       "since_id":0,
       "since_id_str":"0"
    }

最终,我希望能够输出带有发件人的姓名和推文的日期/时间.

Ultimately, I would like to be able to output a list of tweets with the name of the sender and the date/time of the tweet.

我已经通读了 gson 文档,但它让我无法理解说实话 - 对我来说有很多新概念.

I have read through the gson documentation but it's going over my head to be honest - lots of new concepts there for me.

我需要定义一个完全映射到结构的类吗?JSON 以便然后填充该类的实例?如果是这样似乎非常不灵活/费力.理想情况下,我正在寻找一些东西它将以任何形式处理 JSON 并给我一个我可以使用的结构自动...

Do I need to define a class which maps exactly to the structure of the JSON in order to then populate an instance of that class? If so this seems very inflexible/laborious. Ideally I'm looking for something which will handle JSON in any form and give me a structure I can use automatically...

谁能给我指点一下?刚接触这个 - 更多详细,字数越少越好!

Is anyone able to give me some pointers? Being new to this - the more detailed and in words of the fewest syllables the better!

更新 - 由于我已经对此做出了回应,我已经尝试将一个类放在一起来捕获 twitter JSON.然而,由于 JSON 有一个嵌入的 ArrayList 对象我有点挣扎......到目前为止我有

Update - Thanks to the responses I've already had on this I've had a go at putting a class together to capture the twitter JSON. However, since the JSON has an embedded ArrayList of Objects I'm struggling a bit... So far I have

public class tweetData {
    private double completed_in;
    private long max_id;
    private long max_id_str;
    private String next_page;
    private int page;
    private String query;
    private String refresh_url;
    private List<tweetDetails> tweets = new ArrayList<tweetDetails>();
}

public class tweetDetails {
    private String created_at;
    private String from_user;
    private long from_user_id;
    private long from_user_id_str;
    private String from_user_name;
    private String geo;
    private long id;
    private long id_str;
    private String iso_language_code;
//  "metadata":
//  {
//  "result_type":"recent"
//  },
    private String profile_image_url;
    private String profile_image_url_https;
    private String source;
    private String text;
    private String to_user;
    private String to_user_id;
    private String to_user_id_str;
    private String to_user_name;
}

我正在实例化的

URI uri = new URI("http", "search.twitter", "/search.json", "q="+ searchTerms + "&rrp=" + RRP, null);
URL twitterSearch = uri.toURL();
URLConnection yc = twitterSearch.openConnection();
JsonReader reader = new JsonReader(new InputStreamReader(yc.getInputStream()));
Gson gson = new Gson();
tweetData data = gson.fromJson(reader, tweetData.class);
System.out.println(data);

基本名称:值已正确填充,但 ArrayList 未正确填充.

The basic name:values are being populated correctly but the ArrayList is not.

tweetData : 0.17196614959919140865196614959919140865?page=2&max_id=196614959919140865&q=test1test?since_id=196614959919140865&q=testSIZE 0[]

所以,我仍然有点挣扎 - 非常感谢您提供更多提示!

So, I'm still struggling a bit - any more tips hugely appreciated!

蒂亚,汤姆

推荐答案

我是否需要定义一个完全映射到 JSON 结构的类,以便填充该类的实例?如果是这样,这似乎非常不灵活/费力.

Do I need to define a class which maps exactly to the structure of the JSON in order to then populate an instance of that class? If so this seems very inflexible/laborious.

是的.GSON 是一个可用于将 Java 对象转换为其 JSON 表示的库.它还可用于将 JSON 字符串转换为等效的 Java 对象.这真的很强大,因为您可以从 JSON 表示自动实例化 Java 对象.假设您的 JSON 不改变其结构,您只需定义适当的 Java 对象表示一次.

Yes. GSON is a library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. This is really powerful because you can automagically instantiate your Java objects from the JSON representation. Assuming your JSON doesn't change its structure, you only have to define the appropriate Java object representation once.

理想情况下,我正在寻找可以处理任何形式的 JSON 并给我一个可以自动使用的结构的东西......

Ideally I'm looking for something which will handle JSON in any form and give me a structure I can use automatically...

但是,如果您不想要自动序列化/反序列化,请尝试查看更简单的库,例如 java/projects/jsonp.

However, if you don't want automagical serialisation/deserialisation, then try looking at a simpler library such as java/projects/jsonp.

您可以通过查询键从中提取内容:

You can extract stuff from it just by querying the keys:

final JSONObject json = new JSONObject(theJsonString);
final String id = json.getString("max_id");
final JSONArray results = json.getJSONArray("results");
final String user = results.getJSONObject(2).getString("from_user");

这篇关于如何将任意 JSON 转换为 Java 中可用的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 16:46:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/808886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   结构   JSON   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!