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
  • 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. Commented Oct 10, 2018 at 11:15
  • I recommend using Newtonsoft.Json Commented Oct 10, 2018 at 11:21
  • @Dnomyar96 Thanks your comment helps me to see the answer. Commented Oct 10, 2018 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. Commented Oct 10, 2018 at 11:40
  • Possible duplicate of Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?) Commented Oct 10, 2018 at 11:41

2 Answers 2

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.

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

4 Comments

This won't actually help OP. His problem is in the Json string, not the method of deserializing. His method works just as well as this.
@H. Mikhaeljan, please explain array was not working and you change it to list to make it work, I am not getting this thing.
@AnirudhaGupta The list is not making the change it can also just be an array im just more used to work with lists excuse me for the confusing change.
@H.Mikhaeljan ok
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.