Reputation: 2101
I have an android application that is posting a JSON object to a WCF Service. The JSON Object that is posted contains an array property. My problem is that the arrray property is always null when received on the server.
This is the android code for posting:
ObjectMapper mapper = new ObjectMapper();
ArrayList<RespuestaEncuesta> respuestas = new ArrayList<RespuestaEncuesta>(1);
RespuestaEncuesta r = new RespuestaEncuesta();
r.Comentarios = "ASD";
r.GrupoClienteID = UUID.fromString("00000000-0000-0000-0000-000000000000");
r.GrupoID = 1155;
r.Opcion = "2";
respuestas.add(r);
RespuestaWrapper data = new RespuestaWrapper();
data.Respuestas = respuestas;
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
String respuestarJson = mapper.writeValueAsString(data);
String url = config[0] + "/GuardaEncuestas";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
StringEntity tmp = new StringEntity(respuestarJson);
httpPost.setEntity(tmp);
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.execute(httpPost);
Which generates the following json:
{
"RespuestaWrapper" : {
"Respuestas" : [ {
"Comentarios" : "ASD",
"GrupoClienteID" : "00000000-0000-0000-0000-000000000000",
"Opcion" : "2",
"Numero" : 0,
"GrupoID" : 1155.0
} ]
}
}
On the server side my service is defined as follow:
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "GuardaEncuestas",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void GuardaEncuestas(RespuestaWrapper respuestas);
[Serializable, DataContract]
public class RespuestaWrapper
{
[DataMember]
public List<RespuestasWrapped> Respuestas;
}
[Serializable, DataContract]
public class RespuestasWrapped
{
[DataMember(IsRequired = false)]
public double GrupoID { get; set; }
[DataMember(IsRequired = false)]
public Guid GrupoClienteID { get; set; }
[DataMember(IsRequired = false)]
public int Numero { get; set; }
[DataMember(IsRequired = false)]
public string Opcion { get; set; }
[DataMember(IsRequired = false)]
public string Comentarios { get; set; }
}
Every time i call the service I receive the RespuestasWrapped object but the Respuestas propery is null.
Upvotes: 0
Views: 1871
Reputation: 116138
Your message body style is bare (BodyStyle = WebMessageBodyStyle.Bare
). If you produce your json as
{
"Respuestas" : [ {
"Comentarios" : "ASD",
"GrupoClienteID" : "00000000-0000-0000-0000-000000000000",
"Opcion" : "2",
"Numero" : 0,
"GrupoID" : 1155.0
} ]
}
It will work
Upvotes: 2