[WeChat applet] Use the JSON.parse method to parse the returned JSON string into an object

In the WeChat applet, you can use JSON.parsethe method to parse the returned JSON string into an object. Here is sample code to achieve similar functionality:

// 假设 response 是请求返回的 JSON 字符串
const response = '{"result":{"code":200,"message":"Success"}}';

// 解析 JSON 字符串为对象
const data = JSON.parse(response);
const result = data.result;

// 输出解析后的对象
console.log(result);

In the above code, we use JSON.parsethe method to parse the returned JSON string into an object. We can then access properties of the parsed object via dot syntax or square bracket syntax, eg data.result.

Note that the parsed object is an ordinary JavaScript object in the WeChat applet, not a class instance. Therefore, if you need to convert the parsed object to a custom class object, you need to do it manually.

Please make corresponding modifications according to actual needs and the data structure returned by the interface.

Guess you like

Origin blog.csdn.net/gao511147456/article/details/131632786