安卓:解析JSON字符串的双引号内 [英] Android: Parsing JSON string inside of double quotes

查看:662
本文介绍了安卓:解析JSON字符串的双引号内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般用下面的code解析JSON字符串:

I am normally using the following code to parse JSON strings:

JSONObject jobj = new JSONObject(message);
strmsg = jobj.getString("text");

在数据就像下面,它的伟大工程:

when the data is like the below, it works great:

{"emailAddress": "testuser@gmail.com"}  

突然,我打的问题时,其他一些数据排在解析失败,当我调试,我发现该字符串就像如下:

All of a sudden, I am hitting issues when some other data came in. The parse failed and when I debugged, I found out that the string is like the below:

"{\"emailAddress\":\"testuser@gmail.com\"}"

显然,我得到一个JSON字符串,它是双引号内。 2简单的问题: 1)这是正常的,应该的JSONObject能够解析呢? 2)如果不是,有什么推荐的方法来处理这​​个?

Apparently, I am getting a JSON string that is inside of double quotes. 2 quick questions: 1)Is this normal, and should JSONObject be able to parse it? 2)If not, what's the recommended way to deal with this?

推荐答案

这似乎是在JSON产生一个错误。 JSON的确应该在这里抛出一个解析错误。如果你不能改变的方式,JSON字符串生成,则需要手动清理。除去周围的花括号的报价和deescape引号(你可以通过Simplay的通话做     jsonString.replace(\\,\); 虽然这看起来令人费解,排出的是必要的......

This seems to be an error in the JSON generation. JSON should indeed throw a parse error here. If you cannot change the way, the JSON string is generated, you need to clean it up manually. Remove the quotes around the curly braces and deescape the quotes (you could do it by simplay calling jsonString.replace("\\"", "\""); while this looks convoluted, the escaping is necessary...

String test = "\"{\\\"emailAddress\\\":\\\"testuser@gmail.com\\\"}\"";
test.replace("\\\"", "\"");

这篇关于安卓:解析JSON字符串的双引号内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆