1

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

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
NewPHPer
  • 238
  • 6
  • 22
  • 2
    Maybe it's better to use some standard json serializers? Like Newtonsoft.Json from NuGet. It's much more easier to use and have a lot of useful documentation. – VitaliyK Jul 28 '16 at 09:10
  • 3
    If it isn't working for you then please specify why - do you get an error? do you not get the values?... – Gilad Green Jul 28 '16 at 09:12
  • 1
    The `MessageList` in your JSON string is not an array of strings, its an array of arrays of strings. – CS. Jul 28 '16 at 09:20
  • `Login asd = new JavaScriptSerializer().Deserialize(result);` that looks like it should do exactly what you want. So what is the question? – CompuChip Jul 28 '16 at 09:27

3 Answers3

4

Your 'String' is not in fact a String. You should escape the quotes, either using a backslash or using a here string with double double quotes like so;

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

Your data format also does not match your class, you have declared MessageList as an array of Strings, when your data seems to be an array containing another array...

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Milney
  • 6,253
  • 2
  • 19
  • 33
  • First of all thank you for your respose. However I cannot play with the string. It is come from another layer. – NewPHPer Jul 28 '16 at 09:29
  • If it comes from another layer, why are you assigning it to a variable in your example code... please show the String which is returned or we cannot give you a meaningful answer – Milney Jul 28 '16 at 09:32
1

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();
}
CS.
  • 766
  • 8
  • 24
0

Try

Login data = Json.Decode<Login>(result);   

and have a look at msdn and this post.

Community
  • 1
  • 1
  • 1
    Eew, `dynamic`? Why would you _ever_ use that (if it's not COM). At least use the generic version then, `Login data = Json.Decode(result);`. – CompuChip Jul 28 '16 at 09:28
  • Look at the link from msdn. It is only an example of how to use Json.Decode –  Jul 28 '16 at 10:17
  • I looked at the link, it confirms that therre is a generic version. Do you mean to say the example there is better (in which case it would be better to update the example in your post) or that also advocate the use of `dynamic` (because that would surprise me and in fact doesn't seem to be the case). – CompuChip Jul 28 '16 at 10:36