简体   繁体   English

如何使用“ org.json”包中的类将对象序列化为JSON字符串?

[英]How to serialize object to JSON string using classes from “org.json” package?

I've tried to find any useful example to solve this easy task, however, nothing really helpful was found. 我试图找到任何有用的示例来解决此简单任务,但是,没有发现任何真正有用的示例。 I'm at the beginner level in Java and I definitely need some help. 我是Java的初学者,我肯定需要帮助。

I have two simple Java classes: 我有两个简单的Java类:

private class FMList {
    public List<FMItem> items = new ArrayList();
    public long size = 0;
    public int dirs = 0;
    public int files = 0;
    public String path = "";
}

private class FMItem {
    public boolean is_dir = false;
    public String[] ascii = {};
    public String name = "";
    public String mode = "";
    public long size = 0;
    public long mtime = 0;
    public boolean ext = false;
    public String cache = "";
}

My application (applet) creates an instance of FMList , sets data to object properties, and finally serializes the object to JSON string. 我的应用程序(小程序)创建FMList的实例,将数据设置为对象属性,最后将对象序列化为JSON字符串。

First, I tried Google Gson library. 首先,我尝试了Google Gson库。 Simple new Gson().toJson(fmList) did the job perfectly, however my applet failed to start giving exceptions that Gson library has somewhat like security problems. 简单的new Gson().toJson(fmList)完美地完成了这项工作,但是我的applet无法开始给出Gson库有点像安全性问题的例外。 Meanwhile, the applet was signed and all AccessController's were set. 同时,小程序已签名并且所有AccessController都已设置。

Then, I tried to use classes from JSON official website . 然后,我尝试使用JSON官方网站上的类。 There are no exceptions anymore but that simple new JSONObject(fmList).toString() gives {} only. 不再有例外,但是简单的new JSONObject(fmList).toString()仅提供{} I understand that it should be used somehow in a different way. 我知道应该以其他方式使用它。

What is the right way to serialize this simple object to JSON string using classes from "org.json" package? 使用"org.json"包中的类将此简单对象序列化为JSON字符串的正确方法是什么?

Thank you for help! 谢谢你的帮助!

For field access like you are using (not get/set methods) you have to specify them: http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.Object, java.lang.String[]) 对于像您正在使用的字段访问(不是get / set方法),必须指定它们: http : //www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.Object,java。 lang.String [])

You either use the JSON Writer or "Stringer" (write to stream or create String) to not get a pretty printed json. 您可以使用JSON Writer或“ Stringer”(写入流或创建String)来获取精美的json。

http://www.json.org/javadoc/org/json/JSONWriter.html http://www.json.org/javadoc/org/json/JSONWriter.html

http://www.json.org/javadoc/org/json/JSONStringer.html http://www.json.org/javadoc/org/json/JSONStringer.html

Update: Also the class can't be private with the simple org.json lib. 更新:该类也不能通过简单的org.json lib私有。 This works fine: 这工作正常:

public class Main {
    public static class FMList {
        public long size = 0;
        public int dirs = 0;
        public int files = 0;
        public String path = "";
    }

    public static void main(String[] args) {
        System.out.println(new JSONObject(new FMList(), new String[] { "dirs", "files" }).toString());
    }

}

Produces: {"files":0,"dirs":0} 产生: {"files":0,"dirs":0}

Changing to private class produces: {} 改用私人课程会产生: {}

The JSONObject expects to use the getters but your classes are only using public variables instead of the getters: JSONObject期望使用getter,但是您的类仅使用公共变量而不是getter:

JSONObject(java.lang.Object bean) 
      Construct a JSONObject from an Object using bean getters.

http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.Object ) http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject(java.lang.Object

Understood that you are looking specifically for serialization using the org.json package, but if you are open to other libs, Xstream is really easy to use for XML-related tasks, such as JSON serialization. 可以理解,您正在专门使用org.json包进行序列化,但是如果您对其他库开放,那么Xstream确实很容易用于XML相关任务,例如JSON序列化。 Here is a JSON tutorial: http://x-stream.github.io/json-tutorial.html . 这是JSON教程: http : //x-stream.github.io/json-tutorial.html

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

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