对应Newtonsoft.Json.JsonConvert.SerializeObject的Java code(对象源,Newtonsoft.Json.JsonSerializerSettings())在.net中? [英] java code corresponding to Newtonsoft.Json.JsonConvert.SerializeObject(Object source,Newtonsoft.Json.JsonSerializerSettings()) in .net?

查看:1386
本文介绍了对应Newtonsoft.Json.JsonConvert.SerializeObject的Java code(对象源,Newtonsoft.Json.JsonSerializerSettings())在.net中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code在.NET序列化的要求,以JSON格式...的code是这样的。

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

我试图相当于这部分的Java code,但它不能正常工作。

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

推荐答案

从我的理解中,Newtonsoft串行接受一个对象的成员变量和输出的重新presents该对象的JSON字符串。

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

所以,你可以这样做:

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.

好消息是,它是pretty的易于使用。该文件是在这里:

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

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

..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.

这篇关于对应Newtonsoft.Json.JsonConvert.SerializeObject的Java code(对象源,Newtonsoft.Json.JsonSerializerSettings())在.net中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆