1

A web service returns JSON object as blew:

JsonString = "{"d":"[{\"sname\":\"S1\",\"region\":\"R1\",\"name\":\"Q1\"},{\"sname\":\"S2\",\"region\":\"R2\",\"name\":\"Q2\"}]"}"

I tried to Deserialize by doing this:

  1. Define the objects

    public class RootResponseClass
    {
        public ResponseParametersClass[] d { get; set; }
    }
    
    public class ResponseParametersClass
    {
        public string sname { get; set; }
        public string region { get; set; }
        public string name { get; set; }
    }
    
  2. Write the Deserialize Method

     JavaScriptSerializer ser2 = new JavaScriptSerializer();
    
     RootResponseClass obj = new RootResponseClass();
    
     obj = ser2.Deserialize<RootResponseClass>(JsonString);
    

But It is gives error "Cannot convert object of type 'System.String' to type 'NAS.Helpers.ResponseParametersClass[]", So how can i do it!

Solution

 public class RootResponseClass
    {
        public string d { get; set; }
    }

And for deserialize method :

JavaScriptSerializer ser2 = new JavaScriptSerializer();
RootResponseClass obj = new RootResponseClass();
obj = ser2.Deserialize<RootResponseClass>(JsonString);

List<ResponseParametersClass> obj2 = new List<ResponseParametersClass>();
obj2 = ser2.Deserialize<List<ResponseParametersClass>>(obj.d.ToString());
  • 5
    d in the Json doesn't contain an array, but a string. If it was an array, it'd start like [ instead of "[. So it seems like the Json contains another Json string. – Dnomyar96 Oct 10 '18 at 11:15
  • I recommend using Newtonsoft.Json – smolchanovsky Oct 10 '18 at 11:21
  • @Dnomyar96 Thanks your comment helps me to see the answer. – Fatma elzahraa Oct 10 '18 at 11:30
  • I tired the both, and the other answer works too but using another method, but like you said the problem wasn't in the method, it is in how i deal with the json string, so i did what you recommended in comment and it works fine. – Fatma elzahraa Oct 10 '18 at 11:40
  • Possible duplicate of [Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)](https://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe) – DerStarkeBaer Oct 10 '18 at 11:41

2 Answers2

3

You can use the package using Newtonsoft.Json; for deserializing JSON

example

JsonString = "{"d":"[{\"sname\":\"S1\",\"region\":\"R1\",\"name\":\"Q1\"},{\"sname\":\"S2\",\"region\":\"R2\",\"name\":\"Q2\"}]"}";

var foo = JsonConvert.DeserializeObject<RootResponseClass>(JsonString);

foo is your deserialized object.

EDIT

As extra information why the initial way is not working is because your array is starting with quotes so its recognized as a string. After the "{"d": should be just [] instead of "[]"

Thanx Dnomyar96 for pointing that extra out.

H.Mikhaeljan
  • 813
  • 12
  • 22
2

Your Json string seems to contain another Json string. So in order to deserialize this, you'd need to deserialize as you're doing now, but change the ResponseParametersClass to string.

Then you'd need to deserialize the string you just got (as a List<ResponseParametersClass>). So in this case you'd need to deserialize in two seperate steps.

Dnomyar96
  • 300
  • 3
  • 12