如何处理jsonstring

时间:2018-11-21 09:05:58

标签: c# arrays json unity3d helper

我正在通过如下所示的www呼叫接收jsonstrings:

{"Items":[{"Id":xxxxxx,"FirstName":"Carlo","LastName":"Carlosson","RatingAverage":0.0,"Subjects":[{"ID":xxxxxx,"SubjectTitle":"English","LevelTitle":"Some level here"}]},{"Id":xxxxxx,"FirstName":"Dennis","LastName":"Dennisson","RatingAverage":4.3,"Subjects":[{"ID":xxxxxx,"SubjectTitle":"Math","LevelTitle":"High"},{"ID":xxxxxx,"SubjectTitle":"English","LevelTitle":"High}]}]}

然后我像这个小班一样处理它们:

public static class JsonHelper
{
    public static T[] FromJson<T>(string json)
    {
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
        return wrapper.Items;
    }

    public static string ToJson<T>(T[] array)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return JsonUtility.ToJson(wrapper);
    }

    public static string ToJson<T>(T[] array, bool prettyPrint)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return JsonUtility.ToJson(wrapper, prettyPrint);
    }

    [System.Serializable]
    private class Wrapper<T>
    {
        public T[] Items;
    }
} 

我使用这样的类:

Student[] students = JsonHelper.FromJson<Student>(jsonString);

我有以下两种型号:

public class Student
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double RatingAverage { get; set; }
    public List<Subjects> subjects { get; set; }
}

public class Subjects
{
    public int ID { get; set; }
    public string SubjectTitle { get; set; }
    public string LevelTitle { get; set; }
}

现在,JsonHelper完成了一些工作。学生的输出正常,但似乎无法处理学生模型中的嵌套数组/列表(主题)。

我该如何使用它。希望有人可以帮助我,在此先感谢:-)

0 个答案:

没有答案