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