对应Newtonsoft.Json.JsonConvert.SerializeObject的Java code(对象源,Newtonsoft.Json.JsonSerializerSettings()

由网友(单身不香嘛)分享简介:我有一个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 Ne...

我有一个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.

阅读全文

相关推荐

最新文章