Reading and Writing JSON files in JSON. net

Source: Internet
Author: User

Write and read of JSON files

JSON. Net API.

Http://james.newtonking.com/projects/json-net.aspx
Http://www.codeplex.com/json/

The following is the Demo code.

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. io; using system. text; using newtonsoft. JSON; using COM. zjpx. model; using system. collections; using newtonsoft. JSON. converters; namespace web {public partial class testjson: system. web. UI. page {protected void page_load (Object sender, eventargs e) {configfile Model CFM = new configfilemodel (); // entity model class CFM. createdate = "2012-02-23"; CFM. filename = "test.txt"; // you can operate the hashtable ht = new hashtable (); ht. add ("hs_name", "Alex"); ht. add ("hs_pwd", "ggg"); // serialize string JS1 = jsonconvert. serializeobject (CFM); string JS2 = jsonconvert. serializeobject (HT); response. write (JS1); response. write ("<br/>"); response. write (JS2); // "{\" FILENAME \ ": \" test.txt \ ", \" createda Te \ ": \" 2012-02-23 \ "}" // "{\" hs_pwd \ ": \" ggg \ ", \" hs_name \": \ "Alex \"} "// deserialization configfilemodel debc1 = jsonconvert. deserializeobject <configfilemodel> (JS1); configfilemodel debc2 = jsonconvert. deserializeobject <configfilemodel> (JS2); // locate the server physical path // string serverapppath = request. physicalapplicationpath. tostring (); string serverapppath = @ "D: \"; // the configuration file path string con_file_path = @ "" + serverapppath + @ "con Fig. JSON "; if (! File. exists (con_file_path) {file. create (con_file_path);} // write the model data to the file using (streamwriter Sw = new streamwriter (con_file_path) {try {jsonserializer serializer = new jsonserializer (); serializer. converters. add (New javascriptdatetimeconverter (); serializer. nullvaluehandling = nullvaluehandling. ignore; // construct the json.net write stream jsonwriter writer = new jsontextwriter (SW); // serialize model data and write it to serializer in jsonwriter stream of json.net. serialize (writer, CFM); // Ser. serialize (writer, HT); writer. close (); Sw. close () ;}} catch (exception ex) {ex. message. tostring () ;}// read the JSON file using (streamreader sr = new streamreader (con_file_path) {try {jsonserializer serializer = new jsonserializer (); serializer. converters. add (New javascriptdatetimeconverter (); serializer. nullvaluehandling = nullvaluehandling. ignore; // construct the json.net read stream jsonreader reader = new jsontextreader (SR); // deserialize the read json.net reader stream, and load it to the model CFM = serializer. deserialize <configfilemodel> (Reader); response. write ("<br/>"); response. write (CFM. filename + "," + CFM. createdate);} catch (exception ex) {ex. message. tostring () ;}}} public class configfilemodel {public configfilemodel () {}string _ filename; Public String filename {get {return _ filename ;} set {_ filename = value ;}} string _ createdate; Public String createdate {get {return _ createdate ;}set {_ createdate = value ;}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.