Jsonhelper class written by Asp.net

Source: Internet
Author: User

Public class jsonhelper <br/>{< br/> // corresponding JSON singleinfo member <br/> Public String singleinfo = string. empty; </P> <p> protected string _ error = string. empty; <br/> protected bool _ success = true; <br/> protected long _ totalcount = 0; <br/> protected system. collections. arraylist arrdata = new arraylist (); <br/> protected system. collections. arraylist arrdataitem = new arraylist (); </P> <p> Public jsonhelper () <Br/> {<br/> // todo: add the constructor logic here <br/> // <br/>}</P> <p> // success member corresponding to JSON <br/> Public bool success <BR/>{< br/> Get <br/>{< br/> return _ success; <br/>}< br/> set <br/>{< br/> // clear error if it is set to true <br/> If (SUCCESS) _ error = string. empty; <br/> _ success = value; <br/>}</P> <p> // error member corresponding to JSON <br/> Public string error <br/> {<br/> Get <br/>{< br/> return _ error; <br/>}< br /> Set <br/>{< br/> // if an error is set, success is set to false automatically. <br/> If (value! = "") _ Success = false; <br/> _ error = value; <br/>}</P> <p> Public long totlalcount <br/>{< br/> get {return _ totalcount ;} <br/> set {_ totalcount = value ;}< br/>}</P> <p> // reset, this method must be executed every time a JSON object is generated. <br/> Public void reset () <br/>{< br/> _ success = true; <br/> _ error = string. empty; <br/> singleinfo = string. empty; <br/> arrdata. clear (); <br/> arrdataitem. clear (); <br/>}</P> <p> publi C void additem (string name, string value) <br/>{< br/> arrdata. add ("/" "+ name +"/":" + "/" "+ value + "/""); <br/>}</P> <p> Public void itemok () <br/>{< br/> arrdata. add ("<br>"); <br/> // returns the total number of records <br/> totlalcount ++; <br/>}</P> <p> // serialize the JSON object to obtain the returned JSON code. <br/> Public override string tostring () <br/>{< br/> stringbuilder sb = new stringbuilder (); <br/> Sb. append ("{"); <br/> Sb. append ("/" totalc Ount/":/" "+ _ totalcount. tostring () + "/", "); <br/> Sb. append ("/" success/":" + _ success. tostring (). tolower () + ","); <br/> Sb. append ("/" error/":/" "+ _ error. replace ("/" "," // "") + "/", "); <br/> Sb. append ("/" Data/": ["); </P> <p> int Index = 0; <br/> Sb. append ("{"); <br/> If (arrdata. count <= 0) <br/>{< br/> Sb. append ("}"); <br/>}< br/> else <br/>{< br/> foreach (string Val in arrdata) <br/> {<br /> Index ++; </P> <p> If (Val! = "<Br>") <br/>{< br/> Sb. append (Val + ","); <br/>}< br/> else <br/>{< br/> Sb = sb. replace (",", "", sb. length-1, 1); <br/> Sb. append ("},"); <br/> If (index <arrdata. count) <br/>{< br/> Sb. append ("{"); <br/>}</P> <p >}< br/> Sb = sb. replace (",", "", sb. length-1, 1); <br/> Sb. append ("]"); <br/>}</P> <p> Sb. append ("}"); <br/> return sb. tostring (); <br/>}</P> <p> Public String tosingl Estring () <br/>{< br/> stringbuilder sb = new stringbuilder (); <br/> Sb. append ("{"); </P> <p> Sb. append ("success:" + _ success. tostring (). tolower () + ","); </P> <p> Sb. append ("data:"); </P> <p> int Index = 0; <br/> Sb. append ("{"); <br/> If (arrdata. count <= 0) <br/>{< br/> Sb. append ("}"); <br/>}< br/> else <br/>{< br/> foreach (string Val in arrdata) <br/>{< br/> index ++; </P> <p> If (Val! = "<Br>") <br/>{< br/> Sb. append (Val + ","); <br/>}< br/> else <br/>{< br/> Sb = sb. replace (",", "", sb. length-1, 1); <br/> Sb. append ("},"); <br/> If (index <arrdata. count) <br/>{< br/> Sb. append ("{"); <br/>}</P> <p >}< br/> Sb = sb. replace (",", "", sb. length-1, 1); <br/> Sb. append (""); <br/>}</P> <p> Sb. append ("}"); <br/> return sb. tostring (); <br/>}< br/>}

 

Call method:

Jsonhelper JSON = new jsonhelper (); </P> <p> JSON. success = true; </P> <p> JSON. additem ("aid", "1"); </P> <p> JSON. itemok (); </P> <p> JSON. totlalcount = 10; </P> <p> string jsons = ""; <br/> If (JSON. totlalcount> 0) <br/>{< br/> jsons = JSON. tostring (); <br/>}< br/> else <br/>{< br/> jsons = @ "{success: false }"; <br/>}< br/> return jsons;

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.