Home > Net >  Questions about modify the json file in c #
Questions about modify the json file in c #

Time:12-01

I was behind the ajax call ashx program to modify the json file, vs2019 debugging success, deployed to the iis server cannot succeed, also add the handler mapping, does anyone know why?
//ajax methods to submit answers, modify the json file!
//submitted to the server to choose the answer!
$. Ajax ({
//want to use the post way
Type: "Post",
//method in the page and name
Url: "JsonOperate2 ashx,"
/*
Request ashx file to remove the contentType, still have even if
The data format for {" key ", "value"}; Remember not to curly braces outside to add double quotation marks,
It will take less than the ashx page data and failure
*/
//contentType: "application/json; Charset=utf-8,
"DataType: "text",
Data: {
"Key1" : submitStr,
"Key2" : "practice2
"},
Success: the function (data) {
//return data with the data. D for content
//alert (" modify the json file success!" );
},
Error: function (err) {

//alert (err);
}

This is ashx file code

Public class JsonOperate2: IHttpHandler {

Public void the ProcessRequest (HttpContext context) {
The context. The Response. ContentType="text/plain";
//the context. The Response. The Write (" Hello World ");
//from httpRequest value, including the get and post methods
//string str1=context. The Request (" key1 ");//get the answer from, whether it is right!
String practices=context. The Request (" key2 ");//determine a number of questions

The switch (practices)
{
Case "practice1" :
Practice1 (context);
break;
Case "practice2" :
Practice2 (context);
break;
Case "practice3" :
Practice3 (context);
break;
Case "practice4" :
Practice4 (context);
break;
Default:
break;
}

}

Public bool IsReusable {
The get {
return false;
}
}
//method 1
Public void Practice1 (HttpContext context)
{
Strings str1=context. The Request (" key1 ");//get the answer from, whether it is right!
String jsonfile=System. Web. HttpContext. Current. Server MapPath (" Statisticaldata. Json ");
If (the File. The Exists (jsonfile))
{
String jsonString=File. ReadAllText (jsonfile, Encoding. The Default);//read the file
JObject JObject=JObject. Parse (jsonString);//parsed into json
If (str1=="T")
{
//replace required file
Jobject [" statisticalData "] [" T "].=the Convert ToInt32 (jobject [" statisticalData "] [" T "]) + 1;
}
The else {
//replace required file
Jobject [" statisticalData "] [" F "].=the Convert ToInt32 (jobject [" statisticalData "] [" F "]) + 1;
}

String convertString=the Convert. ToString (jobject);//will be changing all the json string
File. WriteAllText (jsonfile convertString);//the contents written into Jon file

}
The else
{

}

}
  •  Tags:  
  • C#
  • Related