Ok, I figured out my original question which was a trivial naming issue, so I will repurpose this question for the actual problem.
I have a class in C#:
class Test
{
public string Foo { get; set; }
}
When I serialize the class with System.Web.Script.Serialization.JavaScriptSerializer, I get:
{"Foo":"Bar"}
In java I have a class:
class Test
{
private String foo;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
I’m attempting to use GSON to parse the JSON:
Gson gson = new Gson();
Test test = gson.fromJson(getJson(), Test.class);
But I get null values.
How can I map the JSON values onto a slightly different class so I can still use best practice naming conventions in each language.
Gson by default uses the field names as-is, so your fields would need to be named
UpTime,ComputerName, etc. From a Java naming perspective that would be blasphemous, though.Keep your fields properly named in Java style as lower camel case and tell Gson to use
FieldNamingPolicy.UPPER_CAMEL_CASEinstead:Alternatively, you could adapt the C# side to output lower camel case
{"upTime" : ... }of course.