簡體   English   中英

解析JSON數組

[英]Parsing JSON array

我正在使用GSON解析JSON響應。

不幸的是,服務器上的WebApi具有非常不典型的JSON對象。

我需要從此JSON解析附件數組(可以有更多附件):

{"htmlMessage":"text","Attachments":{"8216096_0":{"content":null,"filename":"plk.jpg","contentType":"image/jpeg","contentDisposition":"attachment","size":86070}}}

其中8216096_0是附件ID。

我不能用Gson做到這一點(或者我不知道怎么做),所以我想用JSONObjects做到這一點:

// parse attachments
JSONObject attachmentsJson = result.getJSONObject("Attachments");

然后,我有一個帶有附件數組的JSONObject,但是我不知道如何將它們從JSONObject獲取到ArrayList,因為鍵值不是靜態的,而是生成的ID。

謝謝

//編輯:
感謝所有人的幫助! 我的最終解決方案看起來像這樣,尤其要感謝@Jessie A. Morris和他的最終答案!

List<AttachmentModel> attachmentsList = new ArrayList<AttachmentModel>();
for( Map.Entry<String, JsonElement> attachment : attachments.entrySet()) {
    AttachmentModel attachmentModel = new AttachmentModel();
    attachmentModel = gson.fromJson(attachment.getValue().getAsJsonObject().toString(), AttachmentModel.class);;
    attachmentModel.setmUid(attachment.getKey());
    attachmentsList.add(attachmentModel);
 }

好的,我對示例進行了一些更改,並確定它可以正常運行(我剛剛對其進行了測試):

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by jessie on 14-07-09.
 */
public class TestGson {
    private static String JSON = "{\"htmlMessage\":\"text\",\"Attachments\":{\"8216096_0\":{\"content\":null,\"filename\":\"plk.jpg\",\"contentType\":\"image/jpeg\",\"contentDisposition\":\"attachment\",\"size\":86070}}}\n";

    public static void main(String[] args) {
        JsonObject json = new JsonParser().parse(JSON).getAsJsonObject();
        JsonObject attachments = json.getAsJsonObject("Attachments");

        List<JsonObject> attachmentsList = new ArrayList<JsonObject>();
        for( Map.Entry<String, JsonElement> attachment : attachments.entrySet()) {
            attachmentsList.add(attachment.getValue().getAsJsonObject());
        }

        System.out.println("attachmentsList at the end? " + attachmentsList);
    }
}

我不確定這是否真的有效:

final Map<String,JSONObject> attachmentsJson = (Map<String,JSONObject>) jsonArray.getJSONObject("Attachments");
for(String attachmentId : attachmentsJson.keySet()) {
  final JSONObject attachmentJson = attachmentsJson.get(attachmentId);
}

您的示例中的“附件” obj不是數組。

Json數組用[....]表示。

“附件”是一個Json對象,其中包含一個內部對象“ 8216096_0”。

因此,要獲取內部值,請執行以下操作:

JSONObject attachmentsJson = result.getJSONObject("Attachments");
JSONObject inner = attachmentsJson.getJSONObject("8216096_0");

// and interrogate the inner obj:
String content = inner.getString("content");
String filename = inner.getString("filename");


最后,舉例來說,我將添加用於處理(實際)Json數組的代碼:

{"htmlMessage":"text",
   "Attachments":[{"8216096_0":{"content":null,"filename":"plk.jpg","contentType":"image/jpeg",
                  "contentDisposition":"attachment","size":86070}},                  
                 {"8216096_1":{"content":null,"filename":"plk.jpg","contentType":"image/jpeg",
                    "contentDisposition":"attachment","size":86070}},  
              ]
   }


它會像這樣:

JSONArray attachmentsJson = result.getJSONObject("Attachments");
int len = attachmentsJson.length();
for (int i = 0; i < len; i++) {
    JSONObject elem =  attachmentsJson.getJSONObject(i); // <------ get array element 
    JSONObject inner = elem.getJSONObject("8216096_0");   
    // and interrogate the inner obj:
    String content = inner.getString("content");
    String filename = inner.getString("filename");
}

..或類似,取決於您的Json的確切格式。

暫無
暫無

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

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