NewPHPer NewPHPer - 4 months ago 7
JSON Question

Parsing string to an Object C#

I am trying to parse a string into an object

Here is the string;

string result = {"Status":true,"StatusCode":"OK","MessageList":[[1,1,"admin@....net","Google Inc","\/Date(1469685360000)\/","Seatle","Another String"]]}


Here is a class;

[DataContract]
public class Login
{
[DataMember]
public bool Status { get; set; }
[DataMember]
public string StatusCode { get; set; }
[DataMember]
public string[] MessageList { get; set; }

}


Here is the code;

Login asd = new JavaScriptSerializer().Deserialize<Login>(result);


I am new in this field, so I don't have much idea how to code it

C S C S
Answer

The MessageList in your JSON string is not an array of strings, its an array of arrays of strings. Update the MessageList property in the class definition to public string[][] MessageList { get; set; }

[DataContract]
public class Login
{
    [DataMember]
    public bool Status { get; set; }
    [DataMember]
    public string StatusCode { get; set; }
    [DataMember]
    public string[][] MessageList { get; set; }
}

Now, the de-serialization should work.

static void Main(string[] args)
{
    string result = "{\"Status\":true,\"StatusCode\":\"OK\",\"MessageList\":[[1,1,\"admin@....net\",\"Google Inc\",\"\\/Date(1469685360000)\\/\",\"Seatle\",\"Another String\"]]}";
    Login asd = new JavaScriptSerializer().Deserialize<Login>(result);
    Console.ReadLine();
}