简体   繁体   English

如何从 android 中的原始有效载荷中提取价值?

[英]How to extract value from raw payload in android?

I am trying to extract google.sent_time from the following JSON, how do I get that value?我正在尝试从以下 JSON 中提取 google.sent_time,如何获得该值?
It seems like it's a raw payload, and not straightforward JSON, so wondering if there is a way to extract it?看起来它是一个原始有效负载,而不是简单的 JSON,所以想知道是否有办法提取它?

{
  "notificationID": "677291f5-a784-43df-af2e-0067e9c",
  "title": "test 223",
  "body": "payload",
  "lockScreenVisibility": 1,
  "groupMessage": "",
  "fromProjectNumber": "819539062812",
  "priority": 5,
  "rawPayload": "{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1591849563191,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"i\\\":\\\"677291f5-a784-43df-af2e-0363a4067e9c\\\"}\",\"oth_chnl\":\"\",\"pri\":\"5\",\"vis\":\"1\",\"from\":\"819539062812\",\"alert\":\"payload\",\"title\":\"test 223\",\"grp_msg\":\"\",\"google.message_id\":\"0:1591849563205759%409ebbcaf9fd7ecd\",\"google.c.sender.id\":\"819539062812\",\"notificationId\":-1451117355}"
}

It is JSON, with some characters escaped so it can fit in to a string type field in your main JSON.它是 JSON,其中一些字符已转义,因此它可以放入主 JSON 中的字符串类型字段。

You can extract the string value of "rawPayload" field an feed that to whatever JSON parser you are using, or you can use a regex directly on the string to get just the value:您可以将“rawPayload”字段的字符串值提取到您正在使用的任何 JSON 解析器的提要中,或者您可以直接在字符串上使用正则表达式来获取值:

Here is a regex example:这是一个正则表达式示例:

Pattern p = Pattern.compile("google\\.sent_time.*:(\\d+)");
Matcher m = p.matcher(yourInputString);
m.find();
String sentTime = m.group(1);

Get the rawPayload value as string from the original JSON and wrap it around JSONObject again:从原始 JSON 中获取rawPayload值作为字符串,并再次将其包裹在JSONObject周围:

public static void main(String[] args) throws Exception {
    String str = "...";     // JSON string here
    
    JSONObject json = new JSONObject(str);
    
    JSONObject payload = new JSONObject(json.getString("rawPayload"));
    System.out.println(payload.toString());
}

Output: Output:

{
   "google.delivered_priority":"normal",
   "google.sent_time":1591849563191,
   "google.ttl":259200,
   "google.original_priority":"normal",
   "custom":"{\"i\":\"677291f5-a784-43df-af2e-0363a4067e9c\"}",
   "oth_chnl":"",
   "pri":"5",
   "vis":"1",
   "from":"819539062812",
   "alert":"payload",
   "title":"test 223",
   "grp_msg":"",
   "google.message_id":"0:1591849563205759%409ebbcaf9fd7ecd",
   "google.c.sender.id":"819539062812",
   "notificationId":-1451117355
}

The org.json library is used for the example. org.json库用于示例。

Use Gson json parser.使用Gson json 解析器。 What it does is map your json data to POJO class.它的作用是 map 你的 json 数据到 POJO class。 But since you don't care about the other items or you don't wanna use Gson you'll have to have to go:但是,由于您不关心其他项目或者您不想使用 Gson,因此您必须使用 go:

    JSONObject obj = new 
  JSONObject(YOUR_JSON_DATA);
   JSONObject notiObj = 
  obj.getJSONObject("rawPayload");
  String sentTime =  notiObj.getString("google.sent_time);

Cheers!干杯!

rawPayload looks like a valid JSON, you can check this using any online json formatter. rawPayload 看起来像一个有效的 JSON,您可以使用任何在线 json 格式化程序进行检查。 You can try this:你可以试试这个:

val str = "{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1591849563191,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"i\\\":\\\"677291f5-a784-43df-af2e-0363a4067e9c\\\"}\",\"oth_chnl\":\"\",\"pri\":\"5\",\"vis\":\"1\",\"from\":\"819539062812\",\"alert\":\"payload\",\"title\":\"test 223\",\"grp_msg\":\"\",\"google.message_id\":\"0:1591849563205759%409ebbcaf9fd7ecd\",\"google.c.sender.id\":\"819539062812\",\"notificationId\":-1451117355}"

    try {
        val key = "google.sent_time"
        val json = JSONObject(str)
        Log.i(javaClass.simpleName, "key = ${json[key]}")
    } catch (e: Exception) {
        Log.e(javaClass.simpleName, "failed :", e)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM