0

I'm really bad with C# and would like your help doing the following; I am currently passing a Json Array to my WCF webservice. I need to take the json array and insert it into a list. I have no idea to deserialize in C#. Please suggest to me what is the best way of achieving this. My code looks as follows:

  public String UpdateOrderAddress(Stream userInfo)
  {
        try
        {               
           StreamReader reader = new StreamReader(userInfo);
           string JSONdata = reader.ReadToEnd();
           if (JSONdata == null)
           {                    
                return "null";
           }                        

           return JSONdata;     // Success !
        }
        catch (Exception e)
        {
            return e.ToString();
        }
  }

This is the data in the string that I get from the reader

[{"date":"2013-02-22 15:30:374:021","id":"1","description":"test","name":"test"},
"date":"2013-02-25 11:56:926:020","id":"2","description":"ghy","name":"fhh"},
"date":"2013-02-25 11:56:248:026","id":"3","description":"ghfm","name":"run"}]
2

3 Answers 3

2

The code you posted doesn't show how are you trying to deserialize your json string so I don't really follow what's the relevance here but in any case, this is how to deserialize JSON into a concrete C# class.

  1. Create a class that matches the structure of your Javascript objects as so:

    public class Data
    {
      public string  Date {get;set;}
      public int ID {get;set;}
      public string Description {get;set;}
      public string Name {get;set;}
    }
    
  2. Deserialize it using the JavascriptSerializer as so:

    var deserializedData = new JavaScriptSerializer().Deserialize<List<Data>>(jsonString);
    

Note that your original JSON string is incorrectly formatted. It's missing the opening { on each element of the array. It should really be:

[{"date":"2013-02-22 15:30:374:021","id":"1","description":"test","name":"test"}, {"date":"2013-02-25 11:56:926:020","id":"2","description":"ghy","name":"fhh"}, {"date":"2013-02-25 11:56:248:026","id":"3","description":"ghfm","name":"run"}]

Now, if you attempt to deserialize the above, as so:

string json = @"[{""date"":""2013-02-22 15:30:374:021"",""id"":""1"",""description"":""test"",""name"":""test""},
   {""date"":""2013-02-25 11:56:926:020"",""id"":""2"",""description"":""ghy"",""name"":""fhh""},
   {""date"":""2013-02-25 11:56:248:026"",""id"":""3"",""description"":""ghfm"",""name"":""run""}]";

var deserializedData = new JavaScriptSerializer().Deserialize<List<Data>>(json);

You'll get a nice List<Data> back.

Also note that I didn't use a DateTime field for the corresponding date field in your Javascript object, the reason being that your sample dates are not valid DateTimes or at least a DateTime object cannot be created from that string representation. For instance, "15:30:374:021" makes no sense - I would imagine that 374 is the seconds field...

You need to add a reference to System.Web.Extensions to be able to use the JavascriptSerializer.

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

Comments

0

You can create a representative class with your required properties to hold the values and use the JavascriptSerializer class. Call the Deserialize<T> method specifying your type to deserialize the JSON into your code.

Links in class names for reference.

Comments

0

You can use Newtonsoft.Json Library. All you will need to do is:

List<YourClass> yourClassList = JsonConvert.DeserializeObject<List<YourClass>>(JSONdata);

You find more information, even samples here

Comments

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.