如何用多维数组反序列化json文件,将其转换为c#中的对象

本文关键字:转换 对象 何用多 数组 反序列化 文件 json | 更新日期: 2023-09-27 18:15:48

我有以下Json文件

[
    {
        "photos": [
            {
                "photo": "http://example.com/media/origin/11820846/photo_80_4_.jpg",
                "photo_order": 1,
                "caption": "photo_80_4_"
            }
        ],
        "id": "11820846"
    }
],
[
    {
        "photos": [
            {
                "photo": "http://example.com/media/new_images/
   bookingpal/united%20arab%20emirates/
   12564676/product65093-015.jpg",
                "photo_order": 1,
                "caption": ""
            }
        ],
        "id": "12564676"
    }
]

原始文件较长,但基本上是重复的。

使用下面的代码,我可以看到第一个数组的数据,但是当它到达第二个数组时失败。

为什么?

class Program {
    static void Main(string[] args) {
        using (var st = new StreamReader(@"C:'Users'mc'Desktop'photojson.txt")) {
            string Json = st.ReadToEnd();
            List<TVID> IdList = JsonConvert.DeserializeObject<List<TVID>>(Json);
            foreach (var ids in IdList) {
                Console.WriteLine(ids.ID);
                foreach (var myphoto in ids.photos) {
                    Console.WriteLine(myphoto.Photo + "," + myphoto.Photo_order + "," +
                                      myphoto.Caption);
                    Console.Read();
                }
            }
        }
    }
    public class TVPhotos {
        public string Photo { get; set; }
        public string Photo_order { get; set; }
        public string Caption { get; set; }
    }
    public class TVID {
        public string ID { get; set; }
        public List<TVPhotos> photos { get; set; }
    }
}

如何用多维数组反序列化json文件,将其转换为c#中的对象

要修复您在评论中提到的错误("读完JSON内容后遇到的额外文本"),您需要将JSON用方括号括起来,如下所示:

[
    [
        {
            "photos": [
                {
                    "photo": "http://example.com/media/origin/11820846/photo_80_4_.jpg",
                    "photo_order": 1,
                    "caption": "photo_80_4_"
                }
            ],
            "id": "11820846"
        }
    ],
    [
        {
            "photos": [
                {
                    "photo": "http://example.com/media/new_images/bookingpal/united%20arab%20emirates/12564676/product65093-015.jpg",
                    "photo_order": 1,
                    "caption": ""
                }
            ],
            "id": "12564676"
        }
    ]
]

反序列化可以使用List>例如:

using (var st = new StreamReader(@"sample1.json"))
{
    string Json = st.ReadToEnd();
    List<List<TVID>> IdListList = JsonConvert.DeserializeObject<List<List<TVID>>>(Json);
    foreach (var IdList in IdListList)
    {
        foreach (var ids in IdList)
        {
            Console.WriteLine(ids.ID);
            foreach (var myphoto in ids.photos)
            {
                Console.WriteLine(myphoto.Photo + "," + myphoto.Photo_order + "," +
                                  myphoto.Caption);
                Console.Read();
            }
        }
    }
}

查看JSON,我认为单个数组可以完成相同的工作,但如果您不能更改JSON,则上述更改应该使其工作