如何在wcf rest服务中读取json数组

时间:2016-03-11 09:33:05

标签: c# android arrays json wcf

我正在使用wcf rest服务从Android应用程序获取数据,我从android应用程序获取json数组但是当我正在读取数据时它得到数组计数0,抛出错误:解析值时遇到意外字符:R。路径' ',第0行,位置0.而android app获取响应错误是:服务器遇到处理请求的错误。有关详细信息,请参阅服务器日志。

这是我的休息服务:

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "finalProductList", RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare)]
        string finalProductList(Productdetails[] objprod);

public string finalProductList(Productdetails[]  objprod)
        {

                BasicConfigurator.Configure();
                log.Info("finalProductList method fired ");

                log.Info(objprod.Length);
                log.Info(objprod);
                StringBuilder sb = new StringBuilder();
                sb.Append("\"jsonResult\":[");
                int result = 0;
                string json = objprod.ToString();
                log.Info(json);
                JObject rss = JObject.Parse(json);
                log.Info(rss);
                JArray items = (JArray)rss["finalProductList"];
}

我尝试使用object []数组,List也获得相同的计数0和null。

这是json回应:

{
       "objprod": [{
               "username": "Surendra",
               "product_id": "10044",
               "product_mrp": "35.00",
               "prd_entertotalqnty": "77.4",
               "product_name": "DOUBLE TONED MILK (HNB) SACHET 200 ML",
               "prd_remnfreeqnty": "44.75",
               "merchantId": "160000",
               "prd_Total": "775.25",
               "prd_enterfreeqnty": "55.25",
               "prd_enterqnty": "22.15",
               "prd_avlfreeqnty": "100",
               "count_id": "1",
               "merchantname": "ABCD MILK"
       }, {
               "username": "Surendra",
               "product_id": "10011",
               "product_mrp": "20.00",
               "prd_entertotalqnty": "5.5",
               "product_name": "FCM SACHET 500 ML",
               "prd_remnfreeqnty": "4",
               "merchantId": "160000",
               "prd_Total": "70",
               "prd_enterfreeqnty": "2",
               "prd_enterqnty": "3.5",
               "prd_avlfreeqnty": "6",
               "count_id": "2",
               "merchantname": "ABCD MILK"
       }]
} 

我的班级:

[DataContract]     公共类产品详细信息     {         [数据成员]         public int merchant_id {get;组; }

    [DataMember]
    public int merchant_name { get; set; } 

    [DataMember]
    public decimal prd_entertotalqnty { get; set; } 

    [DataMember]
    public int product_id { get; set; } 

    [DataMember]
    public string product_name { get; set; } 

    [DataMember]
    public decimal prd_remnfreeqnty { get; set; } 

    [DataMember]
    public decimal prd_enterfreeqnty { get; set; } 

    [DataMember]
    public decimal prd_enterqnty { get; set; } 

    [DataMember]
    public decimal prd_avlfreeqnty { get; set; } 

    [DataMember]
    public decimal product_mrp { get; set; } 

    [DataMember]
    public decimal prd_Total { get; set; } 

    [DataMember]
    public int count_id { get; set; } 

}

我在这里做错了什么...... 1

1 个答案:

答案 0 :(得分:1)

您将对象列表包装到另一个对象中。 试试这个:

[{
           "username": "Surendra",
           "product_id": "10044",
           "product_mrp": "35.00",
           "prd_entertotalqnty": "77.4",
           "product_name": "DOUBLE TONED MILK (HNB) SACHET 200 ML",
           "prd_remnfreeqnty": "44.75",
           "merchantId": "160000",
           "prd_Total": "775.25",
           "prd_enterfreeqnty": "55.25",
           "prd_enterqnty": "22.15",
           "prd_avlfreeqnty": "100",
           "count_id": "1",
           "merchantname": "ABCD MILK"
   }, {
           "username": "Surendra",
           "product_id": "10011",
           "product_mrp": "20.00",
           "prd_entertotalqnty": "5.5",
           "product_name": "FCM SACHET 500 ML",
           "prd_remnfreeqnty": "4",
           "merchantId": "160000",
           "prd_Total": "70",
           "prd_enterfreeqnty": "2",
           "prd_enterqnty": "3.5",
           "prd_avlfreeqnty": "6",
           "count_id": "2",
           "merchantname": "ABCD MILK"
   }]