unity webgl 获得java数据,接口地址,请求方式为post,请求参数为{ "lineid": 27, "areaid": 22, "starttime": "2023-05-07 09:54:22", "endtime": "2023-06-07 09:54:22", "datatype": 1, "pageSize": 5, "pageNumber": 1 },返回结果为{ "returnCode": 0, "returnMessage": "调用成功", "data": { "total": 15, "list": [ { "id": null, "doorinfo": null, "cityname": null, "linename": null, "metrono": null, "doorid": 17414, "doorno": "222222222222222", "typename": null, "type": null, "faultname": "开门障碍物检测3次,开门障碍物检测2次", "sourcename": null, "happentime": "2023-05-29 00:00:00.000", "dealstatus": null, "countryid": 3, "areaid": 22, "lineid": 27, "metroid": 279, "carriageid": 1738, "firstchar": "H" }, { "id": null, "doorinfo": null, "cityname": null, "linename": null, "metrono": null, "doorid": 13456, "doorno": "HHHT0101101", "typename": null, "type": null, "faultname": "输出口0短路故障,输出口0工作异常,数据总线通信故障,门未经许可离开关锁到位位置故障", "sourcename": null, "happentime": "2023-05-29 00:00:00.000", "dealstatus": null, "countryid": 3, "areaid": 22, "lineid": 27, "metroid": 279, "carriageid": 1738, "firstchar": "H" }, { "id": null, "doorinfo": null, "cityname": null, "linename": null, "metrono": null, "doorid": 13608, "doorno": "HHHT0104101", "typename": null, "type": null, "faultname": "SRAM校验异常", "sourcename": null, "happentime": "2023-05-27 20:44:37.000", "dealstatus": null, "countryid": 3, "areaid": 22, "lineid": 27, "metroid": 282, "carriageid": 1756, "firstchar": "H" }, { "id": null, "doorinfo": null, "cityname": null, "linename": null, "metrono": null, "doorid": 13738, "doorno": "HHHT0102503", "typename": null, "type": null, "faultname": "开门障碍物检测2次", "sourcename": null, "happentime": "2023-05-27 16:51:31.000", "dealstatus": null, "countryid": 3, "areaid": 22, "lineid": 27, "metroid": 280, "carriageid": 1748, "firstchar": "H" }, { "id": null, "doorinfo": null, "cityname": null, "linename": null, "metrono": null, "doorid": 13560, "doorno": "HHHT0103101", "typename": null, "type": null, "faultname": "关门时间过长", "sourcename": null, "happentime": "2023-05-27 14:46:56.000", "dealstatus": null, "countryid": 3, "areaid": 22, "lineid": 27, "metroid": 281, "carriageid": 1750, "firstchar": "H" } ], "pageNum": 1, "pageSize": 5, "size": 19, "startRow": 0, "endRow": 18, "pages": 1, "prePage": 0, "nextPage": 0, "isFirstPage": true, "isLastPage": true, "hasPreviousPage": false, "hasNextPage": false, "navigatePages": 8, "navigatepageNums": [ 1, 2, 3 ], "navigateFirstPage": 1, "navigateLastPage": 1 },想单独获得"doorno"、"faultname"和"happentime"数据,在unity webgl中文本框显示出来的代码怎么写
时间: 2023-07-17 22:55:19 浏览: 103
在 Unity WebGL 中使用 JavaScript 调用接口获取数据,可以参考以下代码:
```c#
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GetData : MonoBehaviour
{
public Text resultText;
IEnumerator Start()
{
// 定义接口地址和请求参数
string url = "http://your-interface-url.com";
string json = "{\"lineid\": 27, \"areaid\": 22, \"starttime\": \"2023-05-07 09:54:22\", \"endtime\": \"2023-06-07 09:54:22\", \"datatype\": 1, \"pageSize\": 5, \"pageNumber\": 1 }";
// 构造请求
UnityWebRequest request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
// 发送请求
yield return request.SendWebRequest();
// 处理返回结果
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log(request.error);
resultText.text = request.error;
}
else
{
// 解析返回的 JSON 数据
string jsonResult = request.downloadHandler.text;
JSONObject jsonObject = new JSONObject(jsonResult);
// 获取需要的数据,将其显示在文本框中
string text = "";
foreach (JSONObject item in jsonObject["data"]["list"].list)
{
text += item["doorno"].str + " - " + item["faultname"].str + " - " + item["happentime"].str + "\n";
}
resultText.text = text;
}
}
}
```
其中,使用 UnityWebRequest 发送 POST 请求,将请求参数放在 JSON 字符串中。解析返回的 JSON 数据,然后遍历需要的数据,将其显示在文本框中。可以根据需要调整显示格式。
阅读全文