2017-09-14 52 views
1

我已經從gmail頭獲得了這種json數據。我如何解析它以獲取「傳遞到」和「接收」在android的價值。提前致謝。從電子郵件標頭解析J​​SON

[ 
    {"name":"Delivered-To","value":"ayuka******[email protected]"}, 

    {"name":"Received","value":"by 10.140.22.233 with SMTP id 96csp129737qgn;  Tue, 12 Sep 2017 14:11:47 -0700 (PDT)"}, 

    {"name":"X-Google-Smtp-Source","value":"ADKCNb5EL+VcU9VEZ4HxoicjzSkTx8DxijwG+0LOR+My5P4fQoiAwNEY8LYBEN/kCq+ITzM43nDg"}, 

    {"name":"X-Received","value":"by 10.129.183.31 with SMTP id v31mr14525436ywh.24.1505250707382;  Tue, 12 Sep 2017 14:11:47 -0700 (PDT)"} 
] 
+0

查看GSON:https://github.com/google/gson。這對於任何類型的JSON庫都非常非常直接。 – fjc

回答

3

你只需要獲得由指數JSON對象,然後在這種情況下

JSONArray yourData = new JSONArray(jsonFromGmail); // Put your data inside a JSONArray 
String name = ""; 

try { 
    for (int i = 0; i < yourData.length(); i++) {//Loop through your JSON array 
     JSONObject jsonobj = null; 
     jsonobj = yourData.getJSONObject(i); //Get JSONObjects by index 
     System.out.println("name : " + i + " = " + jsonobj.getString("name")); 
     name = jsonobj.getString("name"); 
     // Do whatever you want with the string 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

希望它可以幫助

+0

工作很好。只是做了一些更改,以消除無數嘗試捕獲。 – Ayuka

+0

很高興聽到,很高興幫助 –

+0

嘿安德烈....剛剛遇到另一個問題...你能幫我弄到電子郵件正文?我發佈了這個問題,至今沒有人提供幫助。 https://stackoverflow.com/questions/46255530/getting-gmail-body-form-payload – Ayuka

1

這個工程使用GSON(見https://github.com/google/gson的安裝說明和文檔):

String json = "[ {\"name\":\"Delivered-To\",\"value\":\"ayuka******[email protected]\"}, {\"name\":\"Received\",\"value\":\"by 10.140.22.233 with SMTP id 96csp129737qgn;  Tue, 12 Sep 2017 14:11:47 -0700 (PDT)\"}, {\"name\":\"X-Google-Smtp-Source\",\"value\":\"ADKCNb5EL+VcU9VEZ4HxoicjzSkTx8DxijwG+0LOR+My5P4fQoiAwNEY8LYBEN/kCq+ITzM43nDg\"}, {\"name\":\"X-Received\",\"value\":\"by 10.129.183.31 with SMTP id v31mr14525436ywh.24.1505250707382;  Tue, 12 Sep 2017 14:11:47 -0700 (PDT)\"}]"; 

    // parse the JSON string as an array 
    JsonArray array = new Gson().fromJson(json, JsonArray.class); 
    String deliveredTo = ""; 
    String received = ""; 

    // iterate through the items of the array 
    for (int i = 0; i < array.size(); i++) { 
     // parse each item as a JSON object, extract name and value from it 
     JsonObject element = array.get(i).getAsJsonObject(); 
     String name = element.get("name").getAsString(); 
     String value = element.get("value").getAsString(); 
      // look for the relevant fields in name; if we found them, set the according values 
      if (name.equals("Delivered-To")) { 
       deliveredTo = value; 
      } else if (name.equals("Received")) { 
       received = value; 
      } 
    } 
    System.out.println("Delivered to: " + deliveredTo); 
    System.out.println("Received: " + received); 

您使用此生產之前,你當然應該增加一些檢查的JSON和沒有的有效性採取像我這樣的假設(例如:namevalue存在於陣列中的每個項目上)。

+0

謝謝。在此代碼的第2行出現「預計com.google.gson.JsonArray,但是com.google.gson.JsonObject」錯誤。 – Ayuka

0

正常工作得到JSON場「名」。

JSONArray emailHeaderObj = null; 
    String deliveredTo = " "; 
    String received = " "; 
    String Subject = " "; 
    String from = ""; 
    try { 
     emailHeaderObj = new JSONArray(emailHeaderString); 

     for (int i = 0; i < emailHeaderObj.length(); i++) { 

      JSONObject element = emailHeaderObj.getJSONObject(i); 
      String name = element.getString("name"); 
      String value = element.getString("value"); 

      switch(name){ 
       case "Date": 
        received = value; 
        break; 
       case "Subject": 
        Subject = value; 
        break; 
       case "From": 
        from = value; 
        break; 
      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    }