WebService returns data to the foreground to obtain and handle JSON-related problems. webservicejson
Cause:
Record the cause of this problem as a result of an answer to a netizen's question. I encountered this problem when I first came into contact with webservice, but I was not very impressed when I wrote the answer, only when webservice returns values in the background, there is a pitfall. The user asked how to convert the character to JSON. The specific problem is as follows:
Returns the result of ajax.:
<String> {"img": [{"ID": "0", "FileUpName": "A00003.jpg", "UpdateFileUpName": "", "UpLoadPath ":"", "SavePath": "C: \ Users \ yangjinlei \ Desktop \ image \ A00003.jpg", "SaveTime": "", "UserId": "0" },{ "ID ": "0", "FileUpName": "A00003.jpg", "UpdateFileUpName": "", "UpLoadPath": "", "SavePath": "C: \ Users \ yangjinlei \ Desktop \ image \ A00003.jpg "," SaveTime ":" "," UserId ":" 0 "},{" ID ":" 0 ", "FileUpName": "A00003.jpg", "UpdateFileUpName": "", "UpLoadPath": "", "SavePath": "C: \ Users \ yangjinlei \ Desktop \ image \ A00003.jpg "," SaveTime ":" "," UserId ":" 0 "},{" ID ":" 0 ", "FileUpName": "A00003.jpg", "UpdateFileUpName": "", "UpLoadPath": "", "SavePath": "C: \ Users \ yangjinlei \ Desktop \ image \ A00003.jpg "," SaveTime ":" "," UserId ":" 0 "}]} </string>
Convert to JSON format.
Process:
At first glance, it can be seen that the webservice return value is a string in xml format. However, to solve the problem quickly, of course, according to the requirements at the first time, the Code is displayed after the local test is passed:
Var str = "...";
JSON. parse (str. substring (str. indexOf ("{"), str. lastIndexOf ("}") + 1 ));
However, there are always problems with the subject, str. indexOf () will report an error, and it will be a bit confusing; at this time, the subject sent me his webservice code, then ...... the second solution is the most efficient and reasonable method.
The modification method is simple. Change the return value of the ajax Request Method to void, and use Context. Response. Write (json) to return JSON data instead of returning an xml node. As a result, I spent an hour in the morning and finished the work in a few minutes in the afternoon.
Summary:
In the future
Afterwards: When intercepting a string, you should pay attention to the content of the original string. As shown in my above, we can note that \ In the path is blocked, replace (/\/g ,"/")).