格式化来自数组中 Web API 调用的响应返回

Posted

技术标签:

【中文标题】格式化来自数组中 Web API 调用的响应返回【英文标题】:Formatting Response return from Web API call in array 【发布时间】:2020-07-20 09:22:49 【问题描述】:

我有一个函数调用,它以数组的形式返回 SQL 查询的结果。

app.get('/alluser', (req, res) => 
    console.log("All Users")
    user.getAllUsers(function (err, result) 
        if (!err) 
            console.log(result)
            res.send( result )

         else 
            res.send(err.errno + " No record");
        
        res.end()
    );
);
function myFunction() 
    console.log("Going");
    axios.get('http://localhost:8081/alluser')
        .then(function (response) 
            console.log(response.data);
            var msgtxt = JSON.stringify(response.data);
            console.log(JSON.parse(msgtxt));
            //var msgtxt = JSON.parse(msgtxt);
            document.getElementById("demo").innerhtml = msgtxt;
        )
        .catch(function (error) 
            document.getElementById("demo").innerHTML = "Bad response";
        );
    ;

我得到的结果是整个大数组,如下所示。我试图找出一个循环来单独提取所有数据,但我意识到数组可以是任何大小,具体取决于 SQL 查询结果的大小。如下:-

 "result": [ "userid": 3, "username": "Susan Teo", "email": "susan@hotmail.com", "role": "user", "password": "12345678" ,  "userid": 6, "username": "Susanto Morlon", "email": "susanto@melon.com", "role": "user", "password": "12345" ,  "userid": 7, "username": "Jim Wash", "email": "Jimmy@hotmail.com", "role": "user", "password": "123454" ,  "userid": 8, "username": "Vatsun Khun", "email": "vatsun@hotmail.com", "role": "admin", "password": "password" ,  "userid": 9, "username": "Susan Tan", "email": "susantan@hotmail.com", "role": "admin", "password": "123454" ,  "userid": 21, "username": "Ronnie Bowey", "email": "ronnieb@hotmail.com", "role": "admin", "password": "123454" ,  "userid": 23, "username": "Margaret Tan", "email": "magtan@hotmail.com", "role": "admin", "password": "123454" ,  "userid": 25, "username": "Jane Teo", "email": "janeteo@hotmail.com", "role": "admin", "password": "12345" ,  "userid": 27, "username": "Martin Luther", "email": "mluther@good.com", "role": "user", "password": "123456" ,  "userid": 29, "username": "James Hwang", "email": "james@look.com", "role": "admin", "password": "uyghj" ,  "userid": 30, "username": "Donald Trump", "email": "donald@usa.com", "role": "user", "password": "12345" ,  "userid": 31, "username": "Maki", "email": "maki@gmail.com", "role": "user", "password": "12344" ,  "userid": 32, "username": "MakiXXX", "email": "makiato@gmail.com", "role": "user", "password": "12345" ,  "userid": 33, "username": "James Toto", "email": "jamest@hotmail.com", "role": "admin", "password": "12345" ,  "userid": 34, "username": "Ronald Tan", "email": "r99supe@gmail.com", "role": "admin", "password": "12345" ] 

提取所有这些单独记录以便我可以在格式化的 HTML 中显示它们的最佳方法是什么?

【问题讨论】:

您希望如何将数据显示为简单的文本?每一行都是一个新元素,还是不同的格式? 【参考方案1】:

如果我了解您在这里想要实现的目标,这就是我的解决方案: 假设 res 是解析后的响应,请考虑以下内容:

Object.entries(res.result).map(entry => 
        console.log( entry[1]);
    );

这会将结果中的每一行打印在不同的行中。

【讨论】:

以上是关于格式化来自数组中 Web API 调用的响应返回的主要内容,如果未能解决你的问题,请参考以下文章

404 在 CORS 请求中返回,来自 Angular 服务的一些 Web api 调用

来自 ASP NET Web API 的 CORS 阻止 Ajax POST 调用 - 对预检请求的响应未通过访问控制检查

如何格式化来自 Web 服务的返回字符串数组

转换来自 web 服务的 Json 格式响应

在 Android 上调用返回 JSON 响应的 HTTP Web API 调用的最有效方法是啥?

如何使用 RxSwift 和 Alamofire 库调用来自另一个 API 的响应的 API?