Home > Net >  Json escape consult
Json escape consult

Time:04-08

Do the interface of the returned json, but contain special characters, below,

[{" FNumber ":" 1.232.12 ", "FName" : "abcdf wwewe/wewe \ we"}, {" FNumber ":" 232323 ", "FName" : "then loudly"}, {" FNumber ":" 343434 ", "FName" : "WWW"}, {" FNumber ":" 1212 ", "FName" : "then loudly
Sympathy fee QWW
Wew "}, {" FNumber ":" 45354545 ", "FName" : "wewewe wewewe}]

Online to find the method of escape
Public static string ReplaceString (string JsonString)
{
If (JsonString==null) {return JsonString; }
If (JsonString. The Contains (" \ \ "))
{
JsonString=JsonString. Replace (" \ \ ", \ \ \ \ "");
}
If (JsonString. Contains (" \ ""))
{
JsonString=JsonString. Replace (" \ ""," \ \ \ '");
}
If (JsonString. Contains (" \ ""))
{
JsonString=JsonString. Replace (" \ ""," \ \ \ "");
}
//remove the string
enter a newlineJsonString=Regex. Replace (JsonString, @ "[\ n \ r]", "");
JsonString=JsonString. The Trim ();
Return JsonString;
}

But returned as a result, the result directly put the Json parser also impassability, escaped with so?
[{\ "FNumber " : \ "1.232.12 ", \ "FName " : abcdf \ 'wwewe/wewe \ \ \ "we "}, {\ "FNumber " : \ "232323 " and \ "FName " : \ "and " loudly}, {\ "FNumber " : \ "343434 " and \ "FName " : \ WWW \ ""}, {" FNumber \ ": " 1212 \ "and " FName \ ": " then loudly sympathy fee qwwwew \ "}, {\ "FNumber " : \ "45354545 " and \ "FName " : \ "wewewe \ wewewe \}]

CodePudding user response:

You have to deal with the source, not in the replacement after get the json, not too many questions, written by normal web API interface, returns this object collection, automatic is serialized, also do not have what difficulty, you this is the string concatenation? To modify the code, I return the right way is

 
[{" FNumber ":" 1.232.12 ", "FName" : "abcdf wwewe/wewe \ \ we"}, {" FNumber ":" 232323 ", "FName" : "then loudly"}, {" FNumber ":" 343434 ", "FName" : "WWW"}, {" FNumber ":" 1212 ", "FName" : "then loudly \ r \ n sympathy fee QWW \ r \ nwew"}, {" FNumber ":" 45354545 ", "FName" : "wewewe \ wewewe}]

CodePudding user response:

 

{" FNumber ":" 1.232.12 ", "FName" : "abcdf wwewe/wewe \ \ we"},
{" FNumber ":" 232323 ", "FName" : "then loudly"},
{" FNumber ":" 343434 ", "FName" : "WWW"},
{" FNumber ":" 1212 ", "FName" : "then loudly \ r \ n sympathy fee QWW \ r \ nwew"},
{" FNumber ":" 45354545 ", "FName" : "wewewe \ wewewe}]


If it is to get after replacement, the interface of entertainment also calculate, if people want to visit, call names

CodePudding user response:

reference 1st floor HZTLTGG response:
you have to deal with the source, not in the replacement after get the json, not too many questions, written by normal web API interface, returns this object collection, automatic is serialized, also do not have what difficulty, you this is the string concatenation? To modify the code, I return the right way is

 
[{" FNumber ":" 1.232.12 ", "FName" : "abcdf wwewe/wewe \ \ we"}, {" FNumber ":" 232323 ", "FName" : "then loudly"}, {" FNumber ":" 343434 ", "FName" : "WWW"}, {" FNumber ":" 1212 ", "FName" : "then loudly \ r \ n sympathy fee QWW \ r \ nwew"}, {" FNumber ":" 45354545 ", "FName" : "wewewe \ wewewe}]


How the source? I this come from a database query, then use the datatable transformation

CodePudding user response:

reference 1st floor HZTLTGG response:
you have to deal with the source, not in the replacement after get the json, not too many questions, written by normal web API interface, returns this object collection, automatic is serialized, also do not have what difficulty, you this is the string concatenation? To modify the code, I return the right way is

 
[{" FNumber ":" 1.232.12 ", "FName" : "abcdf wwewe/wewe \ \ we"}, {" FNumber ":" 232323 ", "FName" : "then loudly"}, {" FNumber ":" 343434 ", "FName" : "WWW"}, {" FNumber ":" 1212 ", "FName" : "then loudly \ r \ n sympathy fee QWW \ r \ nwew"}, {" FNumber ":" 45354545 ", "FName" : "wewewe \ wewewe}]


1.232.12 abcdf 'wwewe/wewe \ we
232323 and loudly
WWW 343434
1212 and sympathy fee loudly QWW wew
45354545 wewewe "wewewe

refer to the second floor HZTLTGG response:
 

{" FNumber ":" 1.232.12 ", "FName" : "abcdf wwewe/wewe \ \ we"},
{" FNumber ":" 232323 ", "FName" : "then loudly"},
{" FNumber ":" 343434 ", "FName" : "WWW"},
{" FNumber ":" 1212 ", "FName" : "then loudly \ r \ n sympathy fee QWW \ r \ nwew"},
{" FNumber ":" 45354545 ", "FName" : "wewewe \ wewewe}]


If it is to get after replacement, the interface of entertainment also calculate, if people want to visit, will be call names


Realize that the character substitution of before is turn json, not to replace, after
Single quotation marks is not special characters?

CodePudding user response:

Don't know what is your project, normal webapi project, you return to the datatable, directly will automatic serialization, you don't need to deal with

CodePudding user response:

refer to 6th floor HZTLTGG response:
don't know what is your normal webapi project, you return to the datatable, directly will automatic serialization, you don't need to deal with ah

Web service is a little special, request returned in Json format, this is generally how do

CodePudding user response:

More confused, web service returns the XML format, how can request return json?

CodePudding user response:

Normally does not need to escape, we project main return json web service, this is a trend
  •  Tags:  
  • C#
  • Related