1

I am trying to parse a string into an object

Here is the string;

string result = {"Status":true,"StatusCode":"OK","MessageList":[[1,1,"[email protected]","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

4
  • 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. Commented Jul 28, 2016 at 9:10
  • 3
    If it isn't working for you then please specify why - do you get an error? do you not get the values?... Commented Jul 28, 2016 at 9:12
  • 1
    The MessageList in your JSON string is not an array of strings, its an array of arrays of strings. Commented Jul 28, 2016 at 9:20
  • Login asd = new JavaScriptSerializer().Deserialize<Login>(result); that looks like it should do exactly what you want. So what is the question? Commented Jul 28, 2016 at 9:27

3 Answers 3

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,""[email protected]"",""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...

Sign up to request clarification or add additional context in comments.

2 Comments

First of all thank you for your respose. However I cannot play with the string. It is come from another layer.
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
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,\"[email protected]\",\"Google Inc\",\"\\/Date(1469685360000)\\/\",\"Seatle\",\"Another String\"]]}";
    Login asd = new JavaScriptSerializer().Deserialize<Login>(result);
    Console.ReadLine();
}

1 Comment

Thank you for your answer. That's what I am exactly looking for
0

Try

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

and have a look at msdn and this post.

3 Comments

Eew, dynamic? Why would you ever use that (if it's not COM). At least use the generic version then, Login data = Json.Decode<Login>(result);.
Look at the link from msdn. It is only an example of how to use Json.Decode
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.