Reputation: 59
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.
Upvotes: 0
Views: 415
Reputation: 7837
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);
Upvotes: 2