Interchange Of. NET strings and JSON formats

Source: Internet
Author: User
Tags string to json

Recently did a questionnaire survey, a questionnaire to save ten or twenty questions and answers! so, Bo Master in order to lazy, directly to save the answer to the content of the JSON format!

The benefits of course are many! Just one field to save the entire answer! It's exciting to think! haha!

well, gossip less, straight into the chase!

First of all, generally do not save the topic, the request may be only save the answer! For example, the question of the corresponding investigators choose what the answer and so on!

First declare a topic class expreience, of course, if you want to add the topic together, you can declare a few fields, right!

The code is as Follows:

  public classExpreience { public stringEXP1 {Get;Set; }  public stringEXP2 {Get;Set; }  public stringEXP3 {Get;Set; }  public stringEXP4 {Get;Set; }  public stringEXP5 {Get;Set; }  public stringEXP6 {Get;Set; }  public stringEXP7 {Get;Set; }  public stringEXP8 {Get;Set; }  public stringEXP9 {Get;Set; }  public stringEXP10 {Get;Set; } }    

ouke, now I have 10 questions, from the above code can look thick! nonsense, full of face, hum hum ~!

All right, schizophrenic patients!

Declare the topic class, the next thing to change the string to JSON format, pay attention to ha!!! Don't wander!

You hurry up, wordy!

ok, now look at the conversion code:

Hashtable hash =NewHashtable (); hash["EXP1"] ="select a"; hash["EXP2"] ="election of the blogger"; hash["Exp3"] ="go back and keep it good"; hash["EXP4"] ="little white"; hash["EXP5"] ="to Pick it up"; hash["EXP6"] ="little white"; hash["EXP7"] ="go to Baba's shoes"; hash["EXP8"] ="little white"; hash["EXP9"] ="reward your bones"; hash["EXP10"] ="wang Wang wang "; JavaScriptSerializer ser=NewJavaScriptSerializer ();
stringANSJSONSTR = Ser. Serialize (hash);

Declare the Hashtable class, store the value of each question in the form of a key-value pair, and then convert it to a JSON string; remember: exp1 in hash["exp1"] corresponds to expreience in the topic exp1!

Remember to Quote:

using System.Web.Script.Serialization; using system.collections;

All right, that's it. look at the converted Format:

{    "exp4": "small white",    "exp9": "reward you root bones",    "exp6": "small white",    "exp1": "select a" ,    " Exp3 ":" go back to keep good,    "exp8": "small white",    "exp5": "to pick up",    "exp10": "wang Wang wang"  ,    "exp7": "go to Baba's shoes in the mouth"    ,"exp2": "select blogger"}

uh, uh, uh ... Although it is not in order to convert, but definitely the right drop! So embarrassing!!! The Broken machine! oh, Oh ...

okay, look at the status of the database save:

It's so magical, it's a field store! Let you install force, see how you value, hem ~!

value? oh, I'm So scared! I'm stumped!!! hahaha, Tease you!

ouke, now say, How to convert this string into a random value of the type, so that we can take any of the value of a problem!

If you can put it in, you'll be able to release it. You look at it! haha ~

Less wordy, Hurry up! If you don't speak, you'll come in a little urine!

well, gossip, Go on!!!

Conversion back in fact is also very simple, on the code:

public expreience GetData (string jsonstr) {     returntypeof(expreience));}

All right, the conversion is good! What the? How to use it? That's not true, It's stupid! All right, go on, "

  public string getdatatostring (string jsonstr) { if   (string.        IsNullOrEmpty (jsonstr))  return  " = GetData (jsonstr);                           String Datatemp  = "1th question:" + strtemp.exp1 + "2nd question:" + STRTEMP.EXP2 +  "3rd question:" + strtemp.exp3 + "4th:" + strtemp.exp4 + "5th question:" + strtemp.exp5                          + "6th question:" + strtemp.exp6 + "7th question:" + STRTEMP.EXP7 +        "8th question:" + strtemp.exp8 + "9th:" + strtemp.exp9 return   datatemp; }

well, It's in order!

It's so easy! yes, well, yes!

A piece of humble text, forget you enlighten!

Discussion group: 225443677 Welcome harassment, do not call the police, will not be pregnant! really, the added People know!

Thank you!

Interchange Of. NET strings and JSON formats

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.