0

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.

3
  • Why not use newtonsoft.json ? Commented Dec 8, 2016 at 8:17
  • 4
    Is your content even valid json? It looks like you aren't escaping quotes so how do you know when the string ends or its just a quote from the inner connect? Commented Dec 8, 2016 at 8:19
  • Try to validate it here jsonlint.com and than post here the valid JSON Commented Dec 8, 2016 at 8:24

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.