surendra
surendra

Reputation: 63

how to read json array in wcf rest service

I am using wcf rest service to get data from android app,I am getting json array from android app but when i am reading data it getting array count 0 , throwing error :Unexpected character encountered while parsing value: R. Path '', line 0, position 0. And android app getting response error is : server encountered an error processing the request. See server logs for more details.

this is my rest service:

[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"];
}

I tried with object[] array and List also getting same count 0 and null.

This is json response:

{
       "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"
       }]
} 

This my class:

[DataContract] public class Productdetails { [DataMember] public int merchant_id { get; set; }

    [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; } 

}

what i done mistake here..1

Upvotes: 0

Views: 1095

Answers (1)

Lennart-
Lennart-

Reputation: 549

You're wrapping a list of objects into another object. Try this:

[{
           "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"
   }]

Upvotes: 1

Related Questions