2017-07-13 7 views
-1

私はJava初心者です。以前はJSONデータを扱っていませんでした。 ニュースデスクトップアプリケーションを作成する必要があります。そのために、GET リクエストをWebアドレスに送信し、JSON出力を以下のように取得します。JSONをJavaで解析してUIに配置する

****OUTPUT in JSON**** 

    { 
    "status": "ok", 
    "source": "the-hindu", 
    "sortBy": "top", 
    -"articles": [ 
    -{ 
    "author": "AP", 
    "title": "Liu Xiaobo, Chinese dissident who won Nobel prize, dies", 
    "description": "He succumbed to cancer in a hospital while remaining under 
    police custody.", 
    "url": "http://www.thehindu.com/news/international/chinese-nobel-laureate- 
    liu-xiaobo-dies/article19271778.ece", 

    "urlToImage":"http://www.thehindu.com/news/international/Xiaobo", 
    "publishedAt": null 
    }, 

    -{ 
    "author": "Pavan Dahat", 
    "title": "BJP leader beaten for carrying ‘beef’", 
    "description": "A video of the incident that went viral on social media, 
    shows the attackers accusing the man of “ killing a cow.”", 
    "url": "http://www.thehindu.com/news/national/other-states/man-attacked- 
    in- 
    nagpur-for-allegedly-carrying-beef/article19269863.ece", 
    "urlToImage": "http://www.thehindu.com/news/national/other- 
    states/article19269862.ece/ALTERNATES/LANDSCAPE_615/beef1", 
    "publishedAt": null 
    }, 

    ] 
    } 

今私は、このJSONデータを解析し、私はそれを行うことができ、その適切なpositions.Howに画像付き見出しを整理する必要がありますか?

+1

https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java –

+0

ジャクソンのライブラリを使用してJSONを解析できます。 –

+0

https://stackoverflow.com/questions/2591098/how-to-parse-json-in-javaあなたはそれを見ることができます。 – Akash

答えて

0

あなたがjackson2で簡単にこれを行うことができます。

最初のアプリで依存関係としてjackson2追加:

配列リストにそれを保存する例:

//Declare an array list of the object that you want to use  
private List<YourOwnObject> cart = new ArrayList<>(); 

//Create an instance of ObjectMapper 
ObjectMapper mapper = new ObjectMapper(); 

//Third pass your json string to the object mapper 
this.cart = mapper.readValue(yourJsonResponseString, mapper.getTypeFactory().constructCollectionType(List.class, YourOwnObject.class)); 

・ホープ、このことができます! について、