0

I am trying to deserialize a nested json string

   public class recentlySearchedAdd
    {
        public recentlySearchedAdd()
        {
            searchedLocations = new List<recentlySearchedLoc>();
        }
        public string status { get; set; }
        public List<recentlySearchedLoc> searchedLocations { get; set; }
    }

    public class recentlySearchedLoc
    {
        public int id { get; set; }
        public string location { get; set; }

    }

And here is the code to handle to json string

        //dummy json string
        string json = "{\"status\": \"OK\", \"searchedLocations\": [{\"id\": 7, \"location\": \"California\"}, {\"id\": 4, \"location\": \"90007\"}, {\"id\": 3, \"location\": \"New York, NY\"}]}";
        JavaScriptSerializer ser = new JavaScriptSerializer();
        List<recentlySearchedAdd> recentlySearchedAddList = ser.Deserialize<List<recentlySearchedAdd>>(json);
        Response.Write("count:"+recentlySearchedAddList.Count);

The count is 0...what's wrong with this code

1 Answer 1

3

You are close. What serializer returns is recentlySearchedAdd not a list of it.

JavaScriptSerializer ser = new JavaScriptSerializer();
recentlySearchedAdd recentlySearchedAddList = ser.Deserialize<recentlySearchedAdd>(json);
Console.Write("count:" + recentlySearchedAddList.searchedLocations.Count);
Sign up to request clarification or add additional context in comments.

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.