简体   繁体   English

如何在Android / java中将GoogleJsonResponseException转换为JSONObject?

[英]How to convert a GoogleJsonResponseException into a JSONObject in android/java?

In my android app, I am handling GoogleJsonResponseException, and depending on its ResultCode and message I should have different code. 在我的Android应用中,我正在处理GoogleJsonResponseException,并且根据其ResultCode和消息,我应该使用不同的代码。

try {
            //type method here

        } catch (GoogleJsonResponseException j) {


            int satusCode = j.getStatusCode();
            String message = getMessageCode(j.toString());}      

Where getMessage(String s) is a method that returns the exception in this String form: 其中getMessage(String s)是一种以以下String形式返回异常的方法:

{
"code": 409,
"errors": [
    {
        "domain": "global",
        "message": "Error message",
        "reason": "conflict"
    }
],
"message": "Error message"}

And i need the data inside "message". 我需要“消息”中的数据。 So is there a way I could change this string into a JSONObject or make convert the GoogleJsonResponse exception from the beginning to a JSONObject? 那么有什么方法可以将这个字符串更改为JSONObject或将GoogleJsonResponse异常从一开始转换为JSONObject? Or should I parse the string manually? 还是应该手动解析字符串?

Thanks 谢谢

JSONObject has a constructor that accepts a string. JSONObject具有接受字符串的构造函数。 Have you tried that? 你有尝试过吗?

Otherwise, reading the documentation , I see 否则,请阅读文档

j.getDetails().getMessage()

And since it wasn't clear which message field you wanted, here's the nested one. 而且由于不清楚您想要哪个消息字段,因此这是嵌套的消息字段。

j.getDetails().getErrors().get(0).getMessage();

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

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