RE:Deserializing Json String to List Collection shows Null value

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

RE:Deserializing Json String to List Collection shows Null value

venkatesh
Hi,

I am trying to deserialize the JSON String from WCF Rest  Service into C# List collection. But deserialization showing null.

WCF Service

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)]
public String getuserSite(String UserCode)
{
    string s = "";
        _ds = new DataSet();
        try
        {
            _dbObj = new Database();
            if (ConfigurationManager.AppSettings["DB"].ToString() == "S")
            {
                string[] _paramNames = { "@UserCode" };
                object[] _paramvalue = { UserCode };
                _ds = _dbObj.GetDatasetProc("getuserSite", _paramNames, _paramvalue);
            }
            else
            {
                string[] _paramNames = { "UserCode" };
                object[] _paramvalue = { UserCode };
                _ds = _dbObj.GetDatasetProc("getuserSite", _paramNames, _paramvalue);
            }
            return **GetJSONString**(_ds, "Sites");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            _dbObj.Close();
        }
    }
}

GetJSONString Method returns JSON String

[OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json
    )]
    public static string GetJSONString(DataSet Ds, String Name)
    {
        DataTable Dt = Ds.Tables[0];
        if (Name != "")
        {
            Dt.TableName = Name;
        }
        string[] StrDc = new string[Dt.Columns.Count];
        string HeadStr = string.Empty;

        for (int i = 0; i < Dt.Columns.Count; i++)
        {
            StrDc[i] = Dt.Columns[i].Caption;
            HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
        }
        HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
        StringBuilder Sb = new StringBuilder();
        if (Name != "")
        {
            Sb.Append("{\"" + Dt.TableName + "\" : [");
        }
        else
        {
            Sb.Append("[");
        }
        for (int i = 0; i < Dt.Rows.Count; i++)
        {
            string TempStr = HeadStr;
            Sb.Append("{");
            for (int j = 0; j < Dt.Columns.Count; j++)
            {
                TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString().Replace("\r\n", "<br>").Replace("\r", "<br>").Replace("\n", "<br>").Replace("\"",""));
            }
            if (i < Dt.Rows.Count - 1) { Sb.Append(TempStr + "},"); } else { Sb.Append(TempStr + "}"); };
        }
        Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length));
        if (Name != "")
        {
            Sb.Append("]}");
        }
        else
        {
            Sb.Append("]");
        }
              return Sb.ToString();
    }

Json String Output

"\"{\\\"Sites\\\" : [{\\\"EQ_DESC\\\" : \\\"SIERRA India\\\",\\\"EQ_CODE\\\" : \\\"1\\\"},{\\\"EQ_DESC\\\" : \\\"SIERRA Dubai\\\",\\\"EQ_CODE\\\" : \\\"24\\\"},{\\\"EQ_DESC\\\" : \\\"SIERRA Malaysia\\\",\\\"EQ_CODE\\\" : \\\"99\\\"},{\\\"EQ_DESC\\\" : \\\"SIERRA US\\\",\\\"EQ_CODE\\\" : \\\"100\\\"},{\\\"EQ_DESC\\\" : \\\"Mars India\\\",\\\"EQ_CODE\\\" : \\\"110\\\"},{\\\"EQ_DESC\\\" : \\\"???\\\",\\\"EQ_CODE\\\" : \\\"151\\\"},{\\\"EQ_DESC\\\" : \\\"turkey\\\",\\\"EQ_CODE\\\" : \\\"2299\\\"},{\\\"EQ_DESC\\\" : \\\"SIERRA Infosys\\\",\\\"EQ_CODE\\\" : \\\"2331\\\"},{\\\"EQ_DESC\\\" : \\\"Infosys\\\",\\\"EQ_CODE\\\" : \\\"2338\\\"}]}\""

C# Class (Model)

public class Site
{
    public string EQ_DESC { get; set; }
    public string EQ_CODE { get; set; }
}

public class RootObject
{
    public List<Site> Sites { get; set; }
}

Now Deserialising json into List

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            /* Deserializing json data from service */
                            var jsoncontent = reader.ReadToEnd();
                            string s = jsoncontent.ToString();

                        var results = JsonConvert.DeserializeObject<RootObject>(s);
                   }
                   }

Output :

var results = JsonConvert.DeserializeObject<Rootobject>(s);

The above code results retun Null.

Kindly suggest me to resolve the issue.

Thanks

Venkatesh
   
Reply | Threaded
Open this post in threaded view
|

Re: Deserializing Json String to List Collection shows Null value

Sayed Arian Kooshesh
did you assign things like [JsonProperty("username")] ?

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|

Re: Deserializing Json String to List Collection shows Null value

Jonathan Pryor-2
In reply to this post by venkatesh
Please file a bug with a test case at bugzilla.xamarin.com.

Thanks,
- Jon

On Jan 7, 2014, at 7:15 AM, venkatesh <[hidden email]> wrote:

> Hi,
>
> I am trying to deserialize the JSON String from WCF Rest  Service into C#
> List collection. But deserialization showing null.
>
> *WCF Service*
>
>
>
> *GetJSONString Method returns JSON String*
>
>
>
> *Json String Output*
>
>
>
> *C# Class (Model)*
>
>
>
> *Now Deserialising json into List*
>
>
>
> *Output :*
>
> var results = JsonConvert.DeserializeObject<Rootobject>(s);
>
> The above code results retun Null.
>
> Kindly suggest me to resolve the issue.
>
> Thanks
>
> Venkatesh
>
>
>
>
> --
> View this message in context: http://mono-for-android.1047100.n5.nabble.com/RE-Deserializing-Json-String-to-List-Collection-shows-Null-value-tp5713653.html
> Sent from the Mono for Android mailing list archive at Nabble.com.
> _______________________________________________
> Monodroid mailing list
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid