使用GSON解析JSON字符串尽可能简单 [英] Parsing JSON String as simple as possible with GSON

查看:636
本文介绍了使用GSON解析JSON字符串尽可能简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GSON,我怎样才能从多维Json字符串中返回一个键?



以下是多维Json字符串:

  {statusCode:0,statusDescription:OK,data:{user:{id:xxx,company_id: XXX ACCOUNT_TYPE: 启用 的 1: 5, enable_locations:真实的, intuit_user_id:空, NICK_NAME: XXX, is_owner, 1}, session_token :xxx}} 

我想返回session_token键值。



我试过这个:

  class app {
static class响应{
String session_token;

public void getSessionToken(){

String x = {statusCode:0,statusDescription:OK,data:{user { ID:XXX, COMPANY_ID:XXX, ACCOUNT_TYPE: 5, enable_locations:真 intuit_user_id:NULL, NICK_NAME: XXX, is_owner: 1,启用:1},session_token:xxx}}
响应r = new Gson()。fromJson(x,Response.class);
System.out.println(r.session_token);




$ b $ p
$ b

但是,我的 r.session_token 返回null。

解决方案

您需要使用Gson的 JsonParser

  String myJsonString ={\name \: \ john\,\ lastname\:\ smith\}; 
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(myJsonString);
JsonObject jsonObject = element.getAsJsonObject();
String lastName = jsonObject.get(lastname)。getAsString();
System.out.println(lastName);

也就是说,这是否可以为您节省任何实时费用,值得商榷!



(编辑自下面的评论):

  class App {

静态类响应{
字符串姓氏;


public static void main(String [] args){
String myJsonString ={\name \:\john \,\\ \\ lastname\:\ smith\};
响应r = new Gson()。fromJson(myJsonString,Response.class);
System.out.println(r.lastname);


$ / code>

Gson会默默地忽略这样的事实: JSON不像你感兴趣的那样,后来你可能对它感兴趣,在这种情况下,向你的 Response添加字段很简单。类。



由于问题变化而编辑:

您有一个JSON对象。它包含一个字段 data ,其值是一个对象。在 对象的内部,您有一个您感兴趣的字段 session_token

您必须通过解析树导航到该字段,否则您必须创建所有将映射到的Java类。 Java类将类似于(最低限度):

  class Response {
Data data;
}

class Data {
String session_token;
}


Using GSON, how can i return a single key from a Multidimensional Json String?

Here is the Multidimensional Json String:

{"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"}}

I want to return the "session_token" key value.

I'm trying this:

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);
    }
}

But with this, my r.session_token returns null.

解决方案

You would need to use Gson's JsonParser class directly and extract the data from the parse tree:

String myJsonString = "{\"name\":\"john\",\"lastname\":\"smith\"}";
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(myJsonString); 
JsonObject jsonObject = element.getAsJsonObject();
String lastName = jsonObject.get("lastname").getAsString();
System.out.println(lastName);

That said, it's debatable whether this would save you any real time over:

(edited from comments below):

class App {

    static class Response {
        String lastname;
    }

    public static void main(String[] args) {
        String myJsonString = "{\"name\":\"john\",\"lastname\":\"smith\"}";
        Response r = new Gson().fromJson(myJsonString, Response.class);
        System.out.println(r.lastname);
    }
}

Gson will silently ignore the fact that there's more data in the JSON than you're interested in, and later on you might be interested in it, in which case it's trivial to add fields to your Response class.

Edit due to question changing:

You have a JSON object. It contains a field data whose value is an object. Inside that object you have a field session_token that you're interested in.

Either you have to navigate to that field through the parse tree, or you have to create Java classes that all will map to. The Java classes would resemble (at the bare minimum):

class Response {
    Data data;
}

class Data {
    String session_token;
}

这篇关于使用GSON解析JSON字符串尽可能简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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