2017-07-07 16 views
-4

我的JSON看起來像這樣。我只需要在片段的佈局中設置這些值。以便顯示有關此產品的詳細信息。如何將json數據顯示到android佈局中?

{ 
    "adid": 5, 
    "uid": 1130, 
    "userName": "Maëly", 
    "cid": 5, 
    "brand": null, 
    "model": null, 
    "title": "Nummular dermatitis", 
    "description": "Nam sa dea ss " 
    "address": "9795 Debra Center", 
    "price": 1544, 
    "photo1": "../../../img/mobile/oneplus/3t/3t-1.jpg", 
    "photo2": null, 
    "photo3": null, 
    "photo4": null, 
    "ip_add": null, 
    "created": 1493921700000, 
    "expiryDate": null, 
    "published": true, 
    "sold": null, 
    "featured": true, 
    "cond": true, 
    "price_neg": true, 
    "used_for": "8", 
    "views": 1575, 
    "city": "Kathmandu", 
    "landmark": null, 
    "quality": null, 
    "alert": null, 
    "features": [ 

    ] 
} 

+0

添加你的代碼你試過。 –

+0

請參閱下面的鏈接幫助[json to refrofit轉換](https://stackoverflow.com/questions/42623437/parse-json-array-response-using-retrofit-gson) –

+0

MuthuKrishnan先生,我有一個pojo類, ApiInterface和ApiClient以及帶有片段活動的空白片段來顯示結果。我添加了一些代碼。 –

回答

1

做了一步,SpaceBison建議 然後這樣做, 如果例如, JSONSCHEMATOPOJO爲你創建POJO類,然後執行此操作。

try{ 
JSONObject object = new JSONObject(string); 
Gson gson= new Gson(); 
POJO p = gson.fromJson(object,POJO.class)' 
}catch(JSONException){ 
e.printStackStra(); 
} 
0

我建議使用a Gson converter。您可以使用jsonschema2pojo生成Gson POJO定義 - 儘管名稱也可以使用示例JSON。

+0

我已經做到了,現在我很困惑如何使用Json。我不知道下一步該做什麼。 –

1

有沒有看過Retrofit文檔? http://square.github.io/retrofit/

您可以對您的界面創建的服務進行同步或異步調用,並根據您的需要對結果進行處理。如果你想要一個片段中顯示的數據,你可能會做一個異步的調用,當你找回數據(通過Gson轉換器應該自動從JSON轉換到你的數據模型類),你可以例如顯示ListView中的數據對象的列表或其他。