简体   繁体   English

如何使用GSON解析变量类型的json?

[英]How to parse json of variable type with GSON?

I recive object like that 我像那样接受对象

{
  "data": "some data",
  "social": {
    "twitter": "id"
  }
}

This is easly parsed using next classes 使用下一个类可以轻松解析

public class SocialLinks {

@Expose
private String data;
@Expose
private Social social;
}
public class Social {

@Expose
private String twitter;
}

Unfortunatly due to some issues, if social is empty it is returened as array 不幸的是,由于某些问题,如果社交为空,则将其重新排列为数组

{
  "data": "some data",
  "social": [

  ]
}

How can I parse it with gson? 我怎样用gson解析它? (I am not a developer of server side and cannot affect responce meassages) (我不是服务器端的开发人员,不会影响响应消息)

You can do that using these classes. 您可以使用这些类来实现。

SocialLinks.java SocialLinks.java

public class SocialLinks {
    private String data;
    private Social social;
    // Getters && Setters
}

Social.java: Social.java:

public class Social {

    private String twitter;
    // Getters & Setters
}

And here is your main method 这是您的主要方法

public class GsonApp {

    private static final String TEST_JSON = "{\n" +
            "  \"data\": \"some data\",\n" +
            "  \"social\": {\n" +
            "    \"twitter\": \"id\"\n" +
            "  }\n" +
            "}";


    public static void main(String[] args) throws Exception {
        final Gson gson = new GsonBuilder().create();
        // Read Example
        final SocialLinks socialLinks = gson.fromJson(TEST_JSON, SocialLinks.class);
        System.out.println(gson.toJson(socialLinks));

        // Write with null Social 
        final SocialLinks socialLinks1 = new SocialLinks();
        socialLinks1.setData("MyData");
        System.out.println(gson.toJson(socialLinks1));

        // Write with empty Social (social.twitter is null)    
        final SocialLinks socialLinks2 = new SocialLinks();
        socialLinks2.setData("MyData");
        socialLinks2.setSocial(new Social());
        System.out.println(gson.toJson(socialLinks2));

        // Write with full Social
        final SocialLinks socialLinks3 = new SocialLinks();
        socialLinks3.setData("MyData");
        socialLinks3.setSocial(new Social());
        socialLinks3.getSocial().setTwitter("ID");
        System.out.println(gson.toJson(socialLinks3));
    }
}

This will output 这将输出

{"data":"some data","social":{"twitter":"id"}}
{"data":"MyData"}
{"data":"MyData","social":{}}
{"data":"MyData","social":{"twitter":"ID"}}

Update 更新

If you data type changes depending on your application state you may want to create Map object instead of DTO. 如果数据类型根据您的应用程序状态而变化,则可能要创建Map对象而不是DTO。 Here is an example 这是一个例子

private static final String TEST_JSON_2 = "{\n" +
        "  \"data\": \"some data\",\n" +
        "  \"social\": [\n" +
        "  ]\n" +
        "}";

...

    Type type = new TypeToken<Map<String, Object>>(){}.getType();
    final Map<String, Object> socialLinks4 = gson.fromJson(TEST_JSON_2, type);
    System.out.println(socialLinks4);

    final Map<String, Object> socialLinks5 = gson.fromJson(TEST_JSON, type);
    System.out.println(socialLinks5);

This will output 这将输出

{data=some data, social=[]}
{data=some data, social={twitter=id}}

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

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