WebService returns data to the foreground to obtain and handle JSON-related problems. webservicejson

Source: Internet
Author: User

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 ,"/")).

  

  

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.