locked
Deserializing A JSON Array Then Convert Result To .Net Object RRS feed

  • Question

  • User1231829591 posted

    I have tried to deserialize a JSON array and convert the result into a dotnet object using the JavaScriptSerializer 

    List<Dom> styleList = (List<Dom>)jSerializer.Deserialize(jsonString, typeof(List<Dom>))

    It gave me the error

    Type 'System.String' is not supported for deserialization of an array.

    After searching the error above online, I came across a post where someone suggested that this problem could be solved using JSON.Net

    To my disappointment, JSON.Net also gave me an error. I got the error 

    Unexpected character encountered while parsing value: 
    [. Path '[0].Style', line 1, position 22.

    Below are my json string which contains escaped double quotes and my C# code to deserialize the JSON string.

    string jsonString = "[{\"ID\":\"xx\", \"Style\":[{\"font-size\": \" 2em\", \"color\": \"blue\"}]}]"
    Newtonsoft.Json.JsonConvert.PopulateObject(jsonString, styleList);

    I had already verified that the json string is valid in jsonlint.com so at the moment I don't know where I went wrong. 

    Please point out what I am doing incorrectly, thanks in advance for your input.

    Tuesday, September 13, 2016 4:04 PM

Answers

  • User347430248 posted

    Hi ManyTitles,

    you can try to do something like below.

    using System.Web.Script.Serialization;
    
    JavaScriptSerializer oJS = new JavaScriptSerializer();
    RootObject oRootObject = new RootObject();
    oRootObject = oJS.Deserialize<RootObject>(Your JSon String);

    For more information regarding DE serializing visit the link below.

    How to: Serialize and Deserialize JSON Data

    Regards

    Deepak 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 3:14 AM

All replies

  • User1869050296 posted

    response = JObject.Parse(jsonString);

    response.ToObject<YourObjectHere>();

    Wednesday, September 14, 2016 3:58 AM
  • User347430248 posted

    Hi ManyTitles,

    you can try to do something like below.

    using System.Web.Script.Serialization;
    
    JavaScriptSerializer oJS = new JavaScriptSerializer();
    RootObject oRootObject = new RootObject();
    oRootObject = oJS.Deserialize<RootObject>(Your JSon String);

    For more information regarding DE serializing visit the link below.

    How to: Serialize and Deserialize JSON Data

    Regards

    Deepak 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 3:14 AM