java .net中Newtonsoft.Json.JsonConvert.SerializeObject(Object source,Newtonsoft.Json.JsonSerializerSettings())对应的java代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5622049/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:57:27  来源:igfitidea点击:

java code corresponding to Newtonsoft.Json.JsonConvert.SerializeObject(Object source,Newtonsoft.Json.JsonSerializerSettings()) in .net?

java.netjsonserializationblackberry

提问by Tijo K Varghese

I have a code in .net that serializes a request to the json format ... The code is something like this.

我在 .net 中有一个代码,它将请求序列化为 json 格式......代码是这样的。

  var ops = new Newtonsoft.Json.JsonSerializerSettings();
  ops.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  ops.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
  ops.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
  ops.Converters.Add(new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter());

  String strSO = Newtonsoft.Json.JsonConvert.SerializeObject(source,
  bIndent ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None,
  ops);

I tried the java code corresponding to this portion but it doesn't work.

我尝试了与此部分对应的 java 代码,但它不起作用。

回答by AndyT

From my understanding, the Newtonsoft serializer takes an object with member variables and outputs a json string that represents that object.

根据我的理解,Newtonsoft 序列化程序接受一个带有成员变量的对象并输出一个表示该对象的 json 字符串。

So you can do something like:

因此,您可以执行以下操作:

Product product = new Product();

product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string output = JsonConvert.SerializeObject(product);

And you'll get an output string like:

你会得到一个输出字符串,如:

{"Name": "Apple",
"Expiry": "\/Date(1230375600000+1300)\/",
"Price": 3.99,
"Sizes": ["Small", "Medium", "Large"]
}

Now the bad news is that the BlackBerry library that you're using doesn't use reflection to examine the structure of objects it serialises. It is a formatter rather than a serializer.

现在坏消息是您使用的 BlackBerry 库不使用反射来检查它序列化的对象的结构。它是一个格式化程序而不是一个序列化程序。

The good news is that it is pretty easy to use. The documentation is here:

好消息是它非常易于使用。文档在这里:

http://www.blackberry.com/developers/docs/6.0.0api/org/json/me/package-summary.html

http://www.blackberry.com/developers/docs/6.0.0api/org/json/me/package-summary.html

In short, to write an object such as the one above, you would do something like:

简而言之,要编写上述对象,您需要执行以下操作:

 myString = new JSONStringer()
 .object()
     .key("Name")
     .value("Apple")
     .key("Expiry")
     .value("Date("+myDate.getTime()+")")
 .endObject()
 .toString();

..and so on. Note that you are constructing the JSON structure element by element, rather than having the JSON library assume that your object is the exact structure of the data you wish to output.

..等等。请注意,您正在逐个元素构建 JSON 结构,而不是让 JSON 库假设您的对象是您希望输出的数据的确切结构。

Hopefully this will give you some idea of how to proceed.

希望这会给你一些关于如何继续的想法。

回答by Jim Blackler

If your question is "Does anyone know of a Java equivalent to Newtonsoft.Json for .NET for serializing in JSON format?"

如果您的问题是“有谁知道 Java 相当于 Newtonsoft.Json for .NET 用于以 JSON 格式进行序列化?”

Check the bottom of http://json.org

检查http://json.org的底部