c# - json.Net save to file -
i have script saving file sorted, cant code save entry: below
{ "entry": [ { "name": "john" }, { "name": "anna" }, { "name": "peter" } ] }
im using json.net, code below needs add entry: name
string json = jsonconvert.serializeobject(results, formatting.indented); string path = @"c:\inetpub\wwwroot\json\json.net\results.json"; if (!file.exists(path)) { file.writealltext(path, json); } else { file.appendalltext(path, json); }
i haven't been able find json code samples, cheers paul
i managed so!
userinfo results = new userinfo { name = request["name"], }; stringbuilder sb = new stringbuilder(); stringwriter sw = new stringwriter(sb); jsonwriter jsonwriter = new jsontextwriter(sw); jsonwriter.formatting = formatting.indented; jsonwriter.writestartobject(); jsonwriter.writepropertyname("name"); jsonwriter.writevalue(results.name); jsonwriter.writeendobject(); string json = sw.tostring(); jsonwriter.close(); sw.close(); string path = @"c:\inetpub\wwwroot\json\json.net\results.json"; if (!file.exists(path)) { file.writealltext(path, json); } else { file.appendalltext(path, json); } } // create class object hold json value public class userinfo { public string name { get; set; } }
Comments
Post a Comment