Java 对象到 JSONObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9792624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java OBJECT to JSONObject
提问by Ethan Sherr
What I am trying to do is create a toJSONObject()
method which returns the JSONObject that has some data from the object as a JSONObject!
我想要做的是创建一个toJSONObject()
方法,该方法返回具有来自对象的一些数据作为 JSONObject 的 JSONObject!
Here is the method I'd really like to call,
这是我真正想调用的方法,
public JSONObject(java.lang.Object object, java.lang.String[] names)
Where:
在哪里:
object
- An object that has fields that should be used to make a JSONObject.names
- An array of strings, the names of the fields to be obtained from the object.
object
- 一个对象,该对象具有用于创建 JSONObject 的字段。names
- 字符串数组,要从对象中获取的字段的名称。
However, eclipse isn't admitting that this specific constructor call is valid, though there's online documentationfor it.
然而,eclipse 不承认这个特定的构造函数调用是有效的,尽管有它的在线文档。
How can I get this to work for me?
我怎样才能让它为我工作?
采纳答案by Jeffrey Blattman
you have two choices,
你有两个选择,
a. if you want to stick with the standard org.gson libraries, you can write your own
一种。如果你想坚持使用标准的 org.gson 库,你可以编写自己的
public static MyObject fromJson(String json)
public String toJson()
methods for each model object. the implementation of each must use the org.json library to populate the fields of the object, and to build a JSON and from the object's fields, respectively.
每个模型对象的方法。每个的实现都必须使用 org.json 库来填充对象的字段,并分别从对象的字段构建 JSON 和 JSON。
b. use GSON or Hymanson that by design will perform object binding. GSON is simpler, Hymanson is faster.
湾 使用按设计将执行对象绑定的 GSON 或 Hymanson。GSON 更简单,Hymanson 更快。
really, i did a performance eval of all three, and it went 1. org.json, Hymanson, and gson, with gson being ~10x slower. it's not really fair to compare org.json however because it doesn't include object binding code.
真的,我对所有三个进行了性能评估,结果是 1. org.json、Hymanson 和 gson,gson 慢了大约 10 倍。然而,比较 org.json 并不公平,因为它不包含对象绑定代码。
if you have a simple flat model object with direct mapping to json, they are both brain dead simple. if you want custom mappings or have complex structures, you will need to read the docs and write some custom serialization / deserialization code.
如果您有一个直接映射到 json 的简单平面模型对象,那么它们都非常简单。如果您想要自定义映射或具有复杂结构,则需要阅读文档并编写一些自定义序列化/反序列化代码。
回答by Yuri Trukhin
To have functionality of JSON in java you must have JSON-lib. JSON-lib also requires following "JAR" files:
要在 Java 中使用 JSON 功能,您必须拥有 JSON-lib。JSON-lib 还需要以下“JAR”文件:
commons-lang.jar
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
json-lib-2.2.2-jdk15.jar
JSON-lib is a java library for that transforms beans, collections, maps, java arrays and XML to JSON and then for retransforming them back to beans, collections, maps and others.
JSON-lib 是一个 Java 库,用于将 bean、集合、映射、java 数组和 XML 转换为 JSON,然后将它们重新转换回 bean、集合、映射等。
In this example we are going to use JSONObject class for creating an object of JSONObject and then we will print these object value. For using JSONObject class we have to import following package "net.sf.json". To add elements in this object we have used put() method. Here is the full example code of FirstJSONJava.java is as follows:
在这个例子中,我们将使用 JSONObject 类来创建 JSONObject 的对象,然后我们将打印这些对象值。为了使用 JSONObject 类,我们必须导入以下包“net.sf.json”。为了在这个对象中添加元素,我们使用了 put() 方法。下面是 FirstJSONJava.java 的完整示例代码如下:
import net.sf.json.JSONObject;
public class FirstJSONJava
{
public static void main(String args[]) {
JSONObject object=new JSONObject();
object.put("name","Amit Kumar");
object.put("Max.Marks",new Integer(100));
object.put("Min.Marks",new Double(40));
object.put("Scored",new Double(66.67));
object.put("nickname","Amit");
System.out.println(object);
}
}
To run this example you have to follow these few steps as follows:
要运行此示例,您必须按照以下几个步骤操作:
Download JSON-lib jar and other supporting Jars Add these jars to your classpath create and save FirstJSONJava.java Compile it and execute
下载 JSON-lib jar 和其他支持的 jar 将这些 jar 添加到您的类路径中 创建并保存 FirstJSONJava.java 编译并执行
Source Code http://www.roseindia.net/tutorials/json/FirstJSONJava.ziphttp://www.roseindia.net/tutorials/json/jsonobject-java-example.shtml
源代码http://www.roseindia.net/tutorials/json/FirstJSONJava.ziphttp://www.roseindia.net/tutorials/json/jsonobject-java-example.shtml
回答by KappaMax
As suggested before, try Hymanson. It's not only faster, but the POJO conversion from/to JSON spares you of time and additional coding.
如前所述,尝试Hymanson。它不仅速度更快,而且 POJO 与 JSON 之间的转换可以节省您的时间和额外的编码。
GSON is also good, but unfortunately I noted you're doing Android development, HTC messed that one up for us, so if you do decide to use GSON remember to jarjarthe library.
GSON 也不错,但不幸的是我注意到你正在做 Android 开发,HTC 为我们搞砸了,所以如果你决定使用 GSON,请记住 jarjar库。
回答by Zon
You can use:
您可以使用:
new org.json.JSONObject (
object.get(
"subObject").
toString());