0

im using json.net

After deserializing of json to object, i want to get json source of this object

e.g.

objParent:{
  objChild1: {name:"testObj"},
  objChild2: {age: 25}
}

in c# code

public ObjChild1
{
  public string name {get;set;}

  [JsonIgnore]
  public string JsonSource { get; set; }    //objChild1: {name:"testObj"}
}

public ObjChild2
{
  public int age {get;set;}

  [JsonIgnore]
  public string JsonSource { get; set; }   //objChild2: {age: 25}   
}

1 Answer 1

0

I don't have json.net installed but with the standard classes you can simply serialize the individual object back to a JSON string, like this:

...
    public static class JSONHelper
    {
        public static string ToJSONString(this object obj)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }
    }
...

    public ObjChild1
    {
        public string name {get;set;}

        [ScriptIgnore]
        public string JsonSource { get { return this.ToJSONString(); } }
    }    

    public class ObjChild2
    {
        public int age {get;set;}

        [ScriptIgnore]
        public string JsonSource { get { return this.ToJSONString(); } }
    }

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.