14

I have a string:

[
  {
    "key": "key1",
    "value": "{'Time':'15:18:42','Data':'15:18:42'}",
    "duration": 5
  },
  {
    "key": "key1",
    "value": "{'Time':'15:18:42','Data':'15:18:42'}",
    "duration": 5
  }
]

My class in Models:

public class CPacket
{
    public string key { get; set; }
    public string value { get; set; }
    public int duration { get; set; }
}

I use Json.Net, I want to convert string bellow to Json Oject.

CPacket c = JsonConvert.DeserializeObject<CPacket>(strPostData);

But it error:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QuoteAPI.Models.CPacket' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

2 Answers 2

31

Your JSON represents an array of CPacket objects, not just a single object. You need to deserialize into a list.

List<CPacket> list = JsonConvert.DeserializeObject<List<CPacket>>(strPostData);
Sign up to request clarification or add additional context in comments.

Comments

-11
    {
        var url = "http://jsonplaceholder.typicode.com/posts";

        var webClient = new WebClient();
        var responseStr = webClient.DownloadString(url);

        //JObject jResponse = JObject.Parse(responseStr);
        JArray jArray = JArray.Parse(responseStr);


        List<User> userList = new List<User>();

        foreach (var item in jArray)
        {
            User user = new User();

            user.UserID = Convert.ToInt32(item["userId"]);
            user.ID = Convert.ToInt32(item["id"]);
            user.Title = Convert.ToString(item["title"]);
            user.Body = Convert.ToString(item["body"]);

            userList.Add(user);
        }







public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = "http://jsonplaceholder.typicode.com/posts";
        var webClient = new WebClient();
        var responseStr = webClient.DownloadString(url);
        JArray jArray = JArray.Parse(responseStr);



        List<Class1> userList = new List<Class1>();

        foreach (var item in jArray)
        {
            Class1 user = new Class1();


            user.userId = Convert.ToInt32(item["userId"]);
            user.id = Convert.ToInt32(item["id"]);
            user.title = Convert.ToString(item["title"]);
            user.body = Convert.ToString(item["body"]);

            userList.Add(user);
        }
        Repeater1.DataSource = userList;
        Repeater1.DataBind();
        var myClass = _download_serialized_json_data<List<Class1>>(url);
        WebRequest request = WebRequest.Create(url);
        WebResponse ws = request.GetResponse();
        //Stream stream1 = response.GetResponseStream();
        //StreamReader sr = new StreamReader(stream1);
        //string strsb = sr.ReadToEnd();
        //object objResponse = JsonConvert.DeserializeObject(strsb, JSONResponseType);
        DataContractJsonSerializer jsonSerializer =
                new DataContractJsonSerializer(typeof(List<Class1>));
        object objResponse = jsonSerializer.ReadObject(ws.GetResponseStream());
        List<Class1> jsonResponse
        = objResponse as List<Class1>;
        List<Class1> photos = (List<Class1>)jsonSerializer.ReadObject(ws.GetResponseStream());
    }

    private static T _download_serialized_json_data<T>(string url) where T : new()
    {
        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            // attempt to download JSON data as a string
            try
            {
                json_data = w.DownloadString(url);
            }
            catch (Exception) { }
            // if string with JSON data is not empty, deserialize it to class and return its instance
            return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
        }
    }
}



}

1 Comment

Please elaborate on your answer instead of posting a wall of code.

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.