[英]Parse JSON array to Object array
我知道這可能是一個非常微不足道的問題,但我似乎無法在網上找到答案..
想象一下,我有一個低於 JSON
{"userId": "myUser","sites": ["site1", "site2"] }
我想將其解析為 C# object User
,但RestSharp
解析器無法正確解析數組。 我嘗試實施如下sites
:
public string[] sites = new string[5];
public List<string> sites;
但似乎沒有任何效果。
對於反序列化,我使用
JsonDeserializer deserial = new JsonDeserializer();
User newUser = new User();
newUser = deserial.Deserialize<user>(response);
--> 響應是一個IRestResponse
object。
該代碼正確解析了userId
之類的簡單字符串,但它在 arrays 上遇到了困難......
我做錯了什么? RestSharp
不是執行此操作的正確工具嗎?
萬一是 Json 中的 object 陣列
{"userImgs": {"small": "https://myImage.co.uk/small", "large": "https://myImage.co.uk/large"}}
我可以只擁有一個 object 和一個 object 作為屬性,如果我如下實現我的 class,解析器會處理嗎?
Class User{
string userId;
UserImages userImg = new UserImages();
謝謝
我建議使用 newtonsoft。
using Newtonsoft.Json;
class User
{
public string userId { get; set; }
public List<string> sites { get; set; }
}
static void Main(string[] args)
{
string response = "{\"userId\" : \"myUser\",\"sites\" : [\"site1\", \"site2\"] }";
User obj = JsonConvert.DeserializeObject<User>(response);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.