使用GSON,我如何从多维Json字符串返回单个键?使用GSON解析JSON字符串尽可能简单
这里是多维的JSON字符串:
{"statusCode":0,"statusDescription":"OK","data":{"user":{"id":xxx,"company_id":xxx,"account_type":"5","enable_locations":true,"intuit_user_id":null,"nick_name":"xxx","is_owner":"1","enabled":"1"},"session_token":"xxx"}}
我要回了 “session_token” 的键值。
我想这一点:
class app {
static class Response {
String session_token;
}
public void getSessionToken() {
String x = {"statusCode":0,"statusDescription":"OK","data":{"user":{"id":xxx,"company_id":xxx,"account_type":"5","enable_locations":true,"intuit_user_id":null,"nick_name":"xxx","is_owner":"1","enabled":"1"},"session_token":"xxx"}}
Response r = new Gson().fromJson(x, Response.class);
System.out.println(r.session_token);
}
}
但有了这个,我r.session_token返回null。
呵呵?所以你在服务器端手动创建一个JSON字符串,并试图通过提供密钥来获取值? –
@wirey不....我只是做了一个例子。我从Web服务器检索JSON字符串,我需要解析它。但我只需要一个键,没有别的。 – Alexandre
@wirey,什么? getParameter不仅适用于GET和POST参数? getParameter如何帮助我获取页面上的字符串(从Web服务器中获取)?字符串是这样的:'http:// setster.com/api/v2/company/6877/service' – Alexandre