【问题标题】:Parsing JSON from email header从电子邮件标头解析 JSON
【发布时间】:2018-02-23 23:30:46
【问题描述】:

我从 gmail 标头中获得了这种 json 数据。我如何解析它以获取 android 中“已交付”和“已接收”的值。提前致谢。

[
    {"name":"Delivered-To","value":"ayuka******l@gmail.com"},

    {"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)"}   
]

【问题讨论】:

标签: android json gmail-api


【解决方案1】:

在这种情况下,您只需要通过索引获取 JSON 对象,然后获取 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();
 }

希望对你有帮助

【讨论】:

  • 工作正常。只是做了一些更改以删除大量的 try catch。
  • 很高兴听到这个消息,很高兴为您提供帮助
  • 嘿安德烈....刚刚遇到另一个问题...您能帮我获取电子邮件正文吗?我发布了这个问题,到目前为止没有人提供帮助。 stackoverflow.com/questions/46255530/…
【解决方案2】:

这使用 Gson 工作(有关安装说明和文档,请参阅 https://github.com/google/gson):

    String json = "[ {\"name\":\"Delivered-To\",\"value\":\"ayuka******l@gmail.com\"}, {\"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 存在于数组中的每个项目上)。

【讨论】:

  • 谢谢。在此代码的第 2 行出现错误“预期为 com.google.gson.JsonArray 但为 com.google.gson.JsonObject”。
【解决方案3】:

工作正常。

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();
    }

【讨论】:

    猜你喜欢
    • 2016-12-18
    • 2020-09-18
    • 1970-01-01
    • 2015-07-26
    • 2010-09-12
    • 2022-12-17
    • 2012-09-30
    • 2016-06-09
    • 2020-10-25
    相关资源
    最近更新 更多