简体   繁体   中英

C# How to deserialize nested Json data

I have a Json data with two fields which are ID and Content. Content will store another Json data. I want to deserialize the first (outer side) Json only. Is it possible to do that?

{"Json1":
[
{"ID":"123",
"Content":"{"Json2":[{"test1":"234","test2":"456"}]}"}
]}

public class testing
{
    public List<testing2> Json1 { get; set; }
}
public class testing2
{
    public string ID { get; set; }
    public string Content { get; set; }
}

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
testing test= serializer.Deserialize<testing>(JsonData);

I expect the value of test.Json1[0].Content is equal to {"Json2":[{"test1":"234","test2":"456"}]} after deserialization. However, exception "Invalid object passed in, ':' or '}' expected." is prompted for the above code.

As was said above your json is invalid. Escape quotes with \\ ,

var jsonData=@"{
    ""Json1"": [{
    ""ID"": ""123"",
    ""Content"": ""{\""Json2\"":[{\""test1\"": \""234\"",\""test2\"":\""456\""}]}""
    }]
    }";

Here is an example of deserializing with NewtwonJson

var instance = JsonConvert.DeserializeObject<testing>( jsonData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM