gpt4 book ai didi

c# - JsonUtility.FromJson 创建列表<对象>

转载 作者:太空狗 更新时间:2023-10-29 22:21:39 24 4
gpt4 key购买 nike

<分区>

我在将 json 字符串转换为 C# 列表时遇到了一些问题!

这是我从服务器获取的 JSON。

[
{"roundid":1,"coins":700,"created":"2016-03-16 11:13:26","duration":198,"score":765230},
{"roundid":3,"coins":330,"created":"2016-03-16 11:13:56","duration":123,"score":425726},
{"roundid":4,"coins":657,"created":"2016-03-16 11:21:23","duration":432,"score":75384},
{"roundid":8,"coins":980,"created":"2016-03-16 11:23:19","duration":271,"score":827200}
]

然后在我的 C# 程序中,我尝试使用此函数将我的 json 字符串转换为可用的对象

public List<Round> getPlayerRounds(string username)
{
string url = BaseURL + "op=findUserRounds&username=" + username;
var json = new WebClient().DownloadString(url);
RoundDataList rounds = JsonUtility.FromJson<RoundDataList>(json);

List<Round> playerRounds = new List<Round>();
//for (var i = 0; i < rounds.roundList.Count; i++)
for (var i = 0; i < rounds.roundList.Length; i++)
{
RoundData rd = rounds.roundList[i];
Round r = rd.getRound();
playerRounds.Add(r);
}
return playerRounds;
}

这里报错

ArgumentException: JSON must represent an object type.

我环顾四周,没有发现任何有效的方法,尝试了 2-3 种解决方案,甚至尝试编辑我的 php web 服务来创建 JSON 字符串。

我的类(class)看起来像这样

    /* This is previus atempt to solve the issue
[Serializable]
public class RoundDataList
{
public List<RoundData> roundList;
}
*/
[Serializable]
public class RoundDataList
{
public RoundData[] roundList;
}
[Serializable]
public class RoundData
{
public int roundid;
public int coins;
public DateTime created;
public int duration;
public int score;


public Round getRound()
{
Round r = new Round();
r.Roundid = roundid;
r.Coins = coins;
r.Created = created;
r.Duration = duration;
r.Score = score;
return r;
}

}

感谢您阅读这篇长文!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com