c# - unity C#:在网络上的 json 文件中查找内容
问题描述
我只想在通过此API获得的相当长的 JSON 文件中找到某个属性。只想获取电影网址
{
"data" :{
"movies": [
{
"url" : [ENTER URL HERE]
}
]
}
}
只想获取该 URL 并以编程方式将其放入变量中以在浏览器中打开并可能显示在应用程序中
获取代码并使 json 对人类可读的脚本:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Serialization;
using UnityEngine.XR.WSA.Sharing;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GETFromYTS : MonoBehaviour
{
[FormerlySerializedAs("URL")] public string url;
void Start()
{
// A correct website page.
StartCoroutine(GetRequest(url));
//Application.OpenURL(url);
}
IEnumerator GetRequest(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
string[] pages = uri.Split('/');
int page = pages.Length - 1;
if (webRequest.isNetworkError)
{
Debug.Log(pages[page] + ": Error: " + webRequest.error);
}
else
{
string json = JsonConvert.DeserializeObject(webRequest.downloadHandler.text).ToString();
Debug.Log(json);
}
}
}
}
使用 Jetbrains Rider 作为 IDE
完整的 json 以获得额外的信息:
{
"status": "ok",
"status_message": "Query was successful",
"data": {
"movie_count": 1,
"limit": 1,
"page_number": 1,
"movies": [
{
"id": 15813,
"url": "https:\/\/yts.mx\/movie\/sonic-the-hedgehog-2020",
"imdb_code": "tt3794354",
"title": "Sonic the Hedgehog",
"title_english": "Sonic the Hedgehog",
"title_long": "Sonic the Hedgehog (2020)",
"slug": "sonic- the-hedgehog-2020",
"year": 2020,
"rating": 6.6,
"runtime": 99,
"genres": [
"Action",
"Adventure",
"Comedy",
"Family",
"Sci-Fi"
],
"summary": "Based on the global blockbuster videogame franchise from Sega, SONIC THE HEDGEHOG tells the story of the world's speediest hedgehog as he embraces his new home on Earth. In this live-action adventure comedy, Sonic and his new best friend Tom (James Marsden) team up to defend the planet from the evil genius Dr. Robotnik (Jim Carrey) and his plans for world domination. The family-friendly film also stars Tika Sumpter and Ben Schwartz as the voice of Sonic.",
"description_full": "Based on the global blockbuster videogame franchise from Sega, SONIC THE HEDGEHOG tells the story of the world's speediest hedgehog as he embraces his new home on Earth. In this live-action adventure comedy, Sonic and his new best friend Tom (James Marsden) team up to defend the planet from the evil genius Dr. Robotnik (Jim Carrey) and his plans for world domination. The family-friendly film also stars Tika Sumpter and Ben Schwartz as the voice of Sonic.",
"synopsis": "Based on the global blockbuster videogame franchise from Sega, SONIC THE HEDGEHOG tells the story of the world's speediest hedgehog as he embraces his new home on Earth. In this live-action adventure comedy, Sonic and his new best friend Tom (James Marsden) team up to defend the planet from the evil genius Dr. Robotnik (Jim Carrey) and his plans for world domination. The family-friendly film also stars Tika Sumpter and Ben Schwartz as the voice of Sonic.",
"yt_trailer_code": "szby7ZHLnkA",
"language": "English",
"mpa_rating": "PG",
"background_image": "https:\/\/yts.mx\/assets\/images\/movies\/sonic_the_hedgehog_2020\/background.jpg",
"background_image_original": "https:\/\/yts.mx\/assets\/images\/movies\/sonic_the_hedgehog_2020\/background.jpg",
"small_cover_image": "https:\/\/yts.mx\/assets\/images\/movies\/sonic_the_hedgehog_2020\/small-cover.jpg",
"medium_cover_image": "https:\/\/yts.mx\/assets\/images\/movies\/sonic_the_hedgehog_2020\/medium-cover.jpg",
"large_cover_image": "https:\/\/yts.mx\/assets\/images\/movies\/sonic_the_hedgehog_2020\/large-cover.jpg",
"state": "ok",
"torrents": [
{
"url": "https:\/\/yts.mx\/torrent\/download\/77EFB6CF3336FCB8FC3FC67A222F548FF88BF00C",
"hash": "77EFB6CF3336FCB8FC3FC67A222F548FF88BF00C",
"quality": "720p",
"type": "web",
"seeds": 4441,
"peers": 736,
"size": "911.23 MB",
"size_bytes": 955493908,
"date_uploaded": "2020-03-08 19:31:51",
"date_uploaded_unix": 1583692311
},
{
"url": "https:\/\/yts.mx\/torrent\/download\/F3ACFD3979CC1A30CC7F312673CED688CE78CE77",
"hash": "F3ACFD3979CC1A30CC7F312673CED688CE78CE77",
"quality": "1080p",
"type": "web",
"seeds": 6737,
"peers": 1071,
"size": "1.65 GB",
"size_bytes": 1771674010,
"date_uploaded": "2020-03-08 21:15:14",
"date_uploaded_unix": 1583698514
}
],
"date_uploaded": "2020-03-08 19:31:51",
"date_uploaded_unix": 1583692311
}
]
},
"@meta": {
"server_time": 1586605584,
"server_timezone": "CET",
"api_version": 2,
"execution_time": "0 ms"
}
}
解决方案
为什么不使用 Unity 内置的JsonUtility静态类?只需用一个类声明数据的形状并用它转换 JSON。
[Serializable]
public class Movie
{
public string url;
public string title;
public string title_english;
// Include every needed field
}
[Serializable]
public class Data
{
public Movie[] movies;
}
[Serializable]
public class Response
{
public Data data;
}
// ... everywhere you need
Response res = JsonUtility.FromJson<Response>(json);
for (int i = 0; i < res.data.movies.Length; i++)
{
Debug.Log(res.data.movies[i].url);
}
推荐阅读
- c - 使用带有 uint64_t 类型的内置溢出函数
- laravel - 在 Vue js 中从一个详细信息页面到主详细信息页面
- node.js - Bookshelf Node js 问题:必须为设备组件定义一个有效的目标模型 hasMany 关系
- node.js - 如何从服务器显示名称到 angular.js?
- laravel - 从 mongodb 中选择每列都有不同架构的数据
- anylogic - AnyLogic 创建多个 Destination 节点访问的代理
- c# - ConcurrentHeap 实现中的问题
- selenium-chromedriver - Robot Framework 的 Input Text 关键字不起作用
- java - 如何使用 JUnit5 为 Kafka 消息消费者测试 PACT
- c# - 将文件上传到 Sharepoint 文件夹