c# - How to keep JSON value as String? -


this json string:

{"name":"chris","home":[],"children":[{"name":"belle"},{"name":"o"}]} 

i create custom object this:

public class child {     public string name { get; set; } }  public class rootobject {     [datamember]     public string name { get; set; }     [datamember]     public list<object> home { get; set; }     [datamember]     public list<child> children { get; set; } } 

but don't want children list,

i want record/serialize children string, not child. meaning don't mind keeping part: [{"name":"belle"},{"name":"o"}] string, not array/list.

how can that? using datacontractjsonseriliazer.readobject method.

since don't mind using library propose newtonsoft json.net. there classes there (jobject, jarray, etc.) can represent arbitrary json data part of typed object. using in order deserialize large json small part of interesting me. deserialize whole json, modify part important , serialize back, keeping irrelevant part untouched.

here code sample children string, though array in json. important modify content of other fields (name , home) , serialize whole thing back, , children in json output remain array original content.

assuming

using newtonsoft.json; using newtonsoft.json.linq; 

here's code sample:

public class rootobject {     public string name;     public list<object> home;     public jarray children; // don't care children may contain }  class program {     static void main(string[] args)     {         string sourcejson =           @"{""name"":""chris"",""home"":[],""children"":[{""name"":""belle""},{""name"":""o""}]}";         rootobject rootobject = jsonconvert.deserializeobject<rootobject>(sourcejson);         string partasstring = rootobject.children.tostring(formatting.none);         // partasstring now: [{"name":"belle"},{"name":"o"}]     } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -