try {
String json = jsonObject.getJSONObject("default").getString("text");
Log.d(LOG_TAG, "JSON value: " + json);
} catch (JSONException e) {
e.printStackTrace();
}
其中jsonObject =
{ “google.sent_time”:1489972321131 “google.message_id”: “0:1489972321143879%1ad00caef9fd7ecd”, “默认”:“{\ n \“condition \”:\“normal”,\ n \“priority \”:\“normal \”,\ n
\“time_to_live \”:0,\ n \“通知\”:{\ n \“body \”: “通知主体”,\ n \“标题\”:\“TEST TITLE”,\ n
\“icon \”:“ic_launcher”\ n},\ n \“data \”:{\ n
“PriorityLevel”:1,\ n \“text \”:\“TEST TEXT”\ n} \ n}“}}}
现在已经超过一个小时了。如何输出属于键“text”的字符串?
错误日志
03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err: org.json.JSONException:Value {03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:“condition”: “normal”,03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:“priority”:“normal”,03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:“time_to_live”:0, 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
“通知”:{03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:“body”:“通知主体”, 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
“title”:“TEST TITLE”,03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:“icon”: “ic_launcher”03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:},03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:“data”:{03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
“PriorityLevel”:1,03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:“text”:“TEST TEXT”03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:} 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:} at java.lang.String类型的默认值无法转换为JSONObject 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在org.json.JSON.typeMismatch(JSON.java:100)03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:at org.json.JSONObject.getJSONObject(JSONObject.java:613)03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:at com.mysampleapp.PushListenerService.onMessageReceived(PushListenerService.java:126) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在com.google.android.gms.gcm.GcmListenerService.zzo(未知来源) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在com.google.android.gms.gcm.GcmListenerService.zzn(未知来源) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在com.google.android.gms.gcm.GcmListenerService.zzm(未知来源) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在com.google.android.gms.gcm.GcmListenerService.zza(未知来源) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在com.google.android.gms.gcm.GcmListenerService $ 1.run(未知来源) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 03-19 21:37:20.653 7499-7640 / com.amazon.mysampleapp W / System.err:
在java.lang.Thread.run(Thread.java:818)03-19 21:48:29.043 7499-7506 / com.amazon.mysampleapp W / art:暂停所有线程: 9.113ms
答案 0 :(得分:0)
用java中的正则表达式解决。它并不理想,但有效。
String message = getMessage(data);
String pattern = "\"text\".:.\"([^\"]+)”";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(message);
m.find();
Log.d(LOG_TAG, m.group(1));