所以我有這個 JSON,從 UnityWebRequest 回應中檢索:
{
"data":{
"attributes":{
"email":"[email protected]",
"first_name":"XXX",
"full_name":"XXX",
"image_url":"https://c8.patreon.com/2/200/XXX",
"is_email_verified":true,
"last_name":"",
"thumb_url":"https://c8.patreon.com/2/200/XXX",
"url":"https://www.patreon.com/user?u=XXX",
"vanity":null
},
"id":"XXX",
"relationships":{
"memberships":{
"data":[
{
"id":"XXX",
"type":"member"
}
]
}
},
"type":"user"
},
"included":[
{
"attributes":{
"currently_entitled_amount_cents":100,
"last_charge_date":"2021-10-20T10:09:42.000 00:00",
"last_charge_status":"Paid",
"lifetime_support_cents":116,
"patron_status":"active_patron",
"pledge_relationship_start":"2021-10-20T10:09:40.763 00:00"
},
"id":"XXX",
"type":"member"
}
],
"links":{
"self":"https://www.patreon.com/api/oauth2/v2/user/XXX"
}
}
我在決議它時遇到問題,我可以輕松地將屬性作為變數訪問。特別是,我需要獲取 current_entitled_amount_cents 的值。我實際上不知道它是如何作業的,所以請幫助我。我在互聯網上找不到任何這樣做的好例子。
編輯:我用 JsonUtility.FromJson 試過了,但它不起作用。
using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// A correct website page.
StartCoroutine(GetRequest("https://www.example.com"));
// A non-existing page.
StartCoroutine(GetRequest("https://error.html"));
}
// Update is called once per frame
void Update()
{
}
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;
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError(pages[page] ": Error: " webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError(pages[page] ": HTTP Error: " webRequest.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log(pages[page] ":\nReceived: " webRequest.downloadHandler.text);
JsonClass JsonClass = JsonUtility.FromJson<JsonClass>(webRequest.downloadHandler.text);
Debug.Log(JsonClass.included.attributes.currently_entitled_amount_cents);
break;
}
}
}
}
[System.Serializable]
public class JsonClass
{
public Included[] included;
}
[System.Serializable]
public class Included
{
public Attributes attributes;
}
[System.Serializable]
public class Attributes
{
public int currently_entitled_amount_cents;
}
控制臺錯誤
error CS1061: 'Included[]' does not contain a definition for 'attributes' and no accessible extension method 'attributes' accepting a first argument of type 'Included[]' could be found (are you missing a using directive or an assembly reference?)
uj5u.com熱心網友回復:
included是一個由多個元素組成的陣列,Include所以你可能想要
JsonClass.included[0].attributes.currently_entitled_amount_cents
為了簡單地取第一個元素。如果以后有更多專案,則取決于您如何確定要訪問這些元素中的哪些元素。
一般來說,你應該避免命名一個與其型別相同的變數,而不是
JsonClass JsonClass
我寧愿使用
JsonClass jsonClass
uj5u.com熱心網友回復:
最簡單的方法是使用 System.Text.Json 決議器,但我建議安裝 Newtonsoft.Json 決議器
此代碼已在 Visual Studio 中測驗并正常作業:
var json= webRequest.downloadHandler.text;
var objects = JObject.Parse(json);
var amountCents=objects["included"][0]["attributes"]["currently_entitled_amount_cents"];
輸出
100
更復雜的方法是反序列化 json,但在這種情況下,您可以輕松訪問任何屬性,甚至可以使用 Linq 進行搜索
var jsonDeserealized = JsonConvert.DeserializeObject<Data>(json);
var currentlyEntitledAmountCents=jsonDeserealized.Included[0].Attributes.CurrentlyEntitledAmountCents;
班級
public partial class Data
{
[JsonProperty("data")]
public DataClass DataData { get; set; }
[JsonProperty("included")]
public Included[] Included { get; set; }
[JsonProperty("links")]
public Links Links { get; set; }
}
public partial class DataClass
{
[JsonProperty("attributes")]
public DataAttributes Attributes { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("relationships")]
public Relationships Relationships { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class DataAttributes
{
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("full_name")]
public string FullName { get; set; }
[JsonProperty("image_url")]
public Uri ImageUrl { get; set; }
[JsonProperty("is_email_verified")]
public bool IsEmailVerified { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
[JsonProperty("thumb_url")]
public Uri ThumbUrl { get; set; }
[JsonProperty("url")]
public Uri Url { get; set; }
[JsonProperty("vanity")]
public object Vanity { get; set; }
}
public partial class Relationships
{
[JsonProperty("memberships")]
public Memberships Memberships { get; set; }
}
public partial class Memberships
{
[JsonProperty("data")]
public Datum[] Data { get; set; }
}
public partial class Datum
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class Included
{
[JsonProperty("attributes")]
public IncludedAttributes Attributes { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class IncludedAttributes
{
[JsonProperty("currently_entitled_amount_cents")]
public long CurrentlyEntitledAmountCents { get; set; }
[JsonProperty("last_charge_date")]
public DateTimeOffset LastChargeDate { get; set; }
[JsonProperty("last_charge_status")]
public string LastChargeStatus { get; set; }
[JsonProperty("lifetime_support_cents")]
public long LifetimeSupportCents { get; set; }
[JsonProperty("patron_status")]
public string PatronStatus { get; set; }
[JsonProperty("pledge_relationship_start")]
public DateTimeOffset PledgeRelationshipStart { get; set; }
}
public partial class Links
{
[JsonProperty("self")]
public Uri Self { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329220.html
上一篇:從層級視窗中移除游戲物件
