簡體   English   中英

如何從java中的json url讀取數據?

[英]how to read data from json url in java?

我遇到了以下教程: http : //www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

但是本教程顯示了將json文件存儲在用戶PC上時如何將json轉換為Java對象。 當我轉到以下鏈接時,我想做的是:

http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[MyApiKey]&q=Toy+Story+3&page_limit=1

它返回以下JSON數據:

{
  "total": 2,
  "movies": [{
    "id": "770672122",
    "title": "Toy Story 3",
    "year": 2010,
    "mpaa_rating": "G",
    "runtime": 103,
    "critics_consensus": "Deftly blending comedy, adventure, and honest emotion, Toy Story 3 is a rare second sequel that really works.",
    "release_dates": {
      "theater": "2010-06-18",
      "dvd": "2010-11-02"
    },
    "ratings": {
      "critics_rating": "Certified Fresh",
      "critics_score": 99,
      "audience_rating": "Upright",
      "audience_score": 91
    },
    "synopsis": "Pixar returns to their first success with Toy Story 3. The movie begins with Andy leaving for college and donating his beloved toys -- including Woody (Tom Hanks) and Buzz (Tim Allen) -- to a daycare. While the crew meets new friends, including Ken (Michael Keaton), they soon grow to hate their new surroundings and plan an escape. The film was directed by Lee Unkrich from a script co-authored by Little Miss Sunshine scribe Michael Arndt. ~ Perry Seibert, Rovi",
    "posters": {
      "thumbnail": "http://content6.flixster.com/movie/11/13/43/11134356_mob.jpg",
      "profile": "http://content6.flixster.com/movie/11/13/43/11134356_pro.jpg",
      "detailed": "http://content6.flixster.com/movie/11/13/43/11134356_det.jpg",
      "original": "http://content6.flixster.com/movie/11/13/43/11134356_ori.jpg"
    },
    "abridged_cast": [
      {
        "name": "Tom Hanks",
        "characters": ["Woody"]
      },
      {
        "name": "Tim Allen",
        "characters": ["Buzz Lightyear"]
      },
      {
        "name": "Joan Cusack",
        "characters": ["Jessie the Cowgirl"]
      },
      {
        "name": "Don Rickles",
        "characters": ["Mr. Potato Head"]
      },
      {
        "name": "Wallace Shawn",
        "characters": ["Rex"]
      }
    ],
    "alternate_ids": {"imdb": "0435761"},
    "links": {
      "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122.json",
      "alternate": "http://www.rottentomatoes.com/m/toy_story_3/",
      "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json",
      "clips": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/clips.json",
      "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/reviews.json",
      "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/similar.json"
    }
  }],
  "links": {
    "self": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Toy+Story+3&page_limit=1&page=1",
    "next": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Toy+Story+3&page_limit=1&page=2"
  },
  "link_template": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"
}

我想將此數據存儲在Java對象中,然后使用它。 我是Java編程的新手。 謝謝。

我建議使用Gson庫之類的東西來解析Json。
Gson使它非常優雅和簡單。
但是,如果您是新手,建議您仔細閱讀Gson概述

Jackson具有一些內置方法來讀取URL。 您可以嘗試以下操作(使用java.net.URL):

ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(new URL("http://www.mydomain.com/info.json"), User.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM