JsonUtility.FromJson创建列表<object>

时间:2016-05-05 20:20:48

标签: c# json unity3d

将json字符串转换为c#List时遇到一些问题!

这是我从服务器获得的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必须表示对象类型。

我环顾四周,找不到任何有用的东西,试过2-3解决方案甚至尝试编辑我创建JSON字符串的php webservice。

我的课程看起来像这样

    /* 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;
    }

}

感谢您查看这篇长篇文章!

2 个答案:

答案 0 :(得分:7)

Unity的新Json API 支持 Json 数组,这正是您的问题所在。有一种方法可以做到这一点,但再次重新发布是一个漫长的过程。请阅读如何操作here。您正在寻找的是第二个解决方案,即“ 2.多个数据(ARRAY JSON)。”

答案 1 :(得分:4)

创建一个类和另一个持有它的类列表对我有用。

[Serializable]
public class Questions
{
    public string id;
    public string question;
    public string answer1;
    public string answer2;
}

[Serializable]
public class QuestionList
{
    public List<Questions> list;
}

//and

var jstring = "{\"list\":[{\"id\":\"1\",\"question\":\"lorem Ipsome \",\"answer1\":\"yes\",\"answer2\":\"no\"},{\"id\":\"2\",\"question\":\"lorem Ipsome Sit dore iman\",\"answer1\":\"si\",\"answer2\":\"ne\"}]}";

var result = JsonUtility.FromJson<QuestionList>(jstring);

从列表中,转换为JSON

List<Questions> questionlist = new List<Questions>();
questionlist.Add(new Questions()
{
    answer1 = "yes",
    answer2 = "no",
    id = "1",
    question = "lorem Ipsome "
});

questionlist.Add(new Questions()
{
    answer1 = "si",
    answer2 = "ne",
    id = "2",
    question = "lorem Ipsome Sit dore iman"
});

var ql = new QuestionList();
ql.list = questionlist;


var result = JsonUtility.ToJson(ql);  

//this gives
{"list":[{"id":"1","question":"lorem Ipsome ","answer1":"yes","answer2":"no"},{"id":"2","question":"lorem Ipsome Sit dore iman","answer1":"si","answer2":"ne"}]}

注意:“list”和questionList.list应该是同名的。