【问题标题】:How do I convert a JSON String into a GSON Object?如何将 JSON 字符串转换为 GSON 对象?
【发布时间】:2015-05-26 08:38:47
【问题描述】:

我要开发一个 web 服务,它的输入应该用 JSON 描述,我想在我的内部逻辑中使用它作为 GSON 对象。因此,无论谁使用该网络服务,都会发送一个包含如下信息的请求:

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
 }
}

我现在需要的是处理它。类似于以下内容:(这只是我如何想象以下步骤以使其运行的示例;))

public class AnyClass{

    public AnyClass(String jsonString){
        GSONObject gobject = new GSONObject(jsonString);
        String title = gobject.getValueOf("title");
    }
}

感谢您的每一个帮助:)

【问题讨论】:

    标签: java json string object gson


    【解决方案1】:

    试试这个

    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(jsonString);
    

    使用 gson

    Gson gson = new Gson();
    gson.fromJson(jsonString, YourBean.class);
    

    【讨论】:

    • 好吧,也许我应该添加一些信息...我尝试将带有 url 作为 QueryParam 的 json 字符串传输并抛出“错误请求”...之前没有测试它:x
    • 不要将其作为 QueryParam 传递,而是尝试使用 $.ajax() 的 data 属性。
    • 我在 JavaDoc 中搜索了一个小时后才找到这个答案,然后通过查看 Gson 源代码最终自己弄清楚了。在这个旅程之后,我想写我自己的更完整的;但后来我找到了这个答案。呃。
    • 对不起@JonasDralle
    【解决方案2】:

    您可以使用 javax.json 包来处理 json 数据。详情见https://docs.oracle.com/javaee/7/tutorial/jsonp002.htm

    【讨论】:

      【解决方案3】:

      将json字符串转成Gson(反序列化)

      使用 gson.fromJson();

      使用字符串类的示例

      Gson gson = new Gson();
      String str = gson.fromJson("\"abc\"", String.class);
      

      使用用户定义类的示例

      BagOfPrimitives obj = new BagOfPrimitives();
      Gson gson = new Gson();
      BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
      

      Gson 的谷歌帮助页面有很好的解释。

      https://sites.google.com/site/gson/gson-user-guide#TOC-Primitives-Examples

      对象示例

      class BagOfPrimitives {
        private int value1 = 1;
        private String value2 = "abc";
        private transient int value3 = 3;
        BagOfPrimitives() {
          // no-args constructor
        }
      }
      

      (序列化)

      BagOfPrimitives obj = new BagOfPrimitives();
      Gson gson = new Gson();
      String json = gson.toJson(obj);  
      ==> json is {"value1":1,"value2":"abc"}
      

      请注意,您不能使用循环引用序列化对象,因为这将导致无限递归。

      (反序列化)

      BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   
      ==> obj2 is just like obj
      

      对象更精细

      It is perfectly fine (and recommended) to use private fields 
      There is no need to use any annotations to indicate a field is to be included for serialization and deserialization. All fields in the current class (and from all super classes) are included by default. 
      If a field is marked transient, (by default) it is ignored and not included in the JSON serialization or deserialization.
      
      This implementation handles nulls correctly
      
      While serialization, a null field is skipped from the output
      
      While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null
      
      If a field is synthetic, it is ignored and not included in JSON serialization or deserialization
      
      Fields corresponding to the outer classes in  inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization
      

      如果您需要更多示例,请查看以下链接

      http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

      http://filotechnologia.blogspot.it/2013/09/convert-java-object-from-json-gson-api.html

      【讨论】:

        猜你喜欢
        • 2021-10-04
        • 1970-01-01
        • 2017-08-11
        • 2018-11-16
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 2017-01-24
        • 1970-01-01
        相关资源
        最近更新 更多