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

发布于 2024-10-31 17:15:06 字数 633 浏览 1 评论 0原文

我在 .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);

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

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

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹倦 2024-11-07 17:15:07

如果您的问题是“有人知道 Java 中相当于 .NET 的 Newtonsoft.Json 的 JSON 格式序列化吗?”

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

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

Check the bottom of http://json.org

故人如初 2024-11-07 17:15:06

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

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

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);

您将得到如下输出字符串:

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

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

好消息是它非常容易使用。文档位于:

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

简而言之,要编写一个像上面这样的对象,您可以执行以下操作

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

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

希望这能让您了解如何继续。

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

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.

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文