简体   繁体   中英

How to deserialize a json file with multidimensional array to convert it to object in c#

I have the following Json file

[
    {
        "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"
    }
]

The original file is longer but basically it repeats.

With the following code I can see the data for the first array but it fails when it gets to the second array.

Why?

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

To fix the error you mentioned in the comments ("Additional text encountered after finished reading JSON content") you need to surround the JSON with square brackets as below:

[
    [
        {
            "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"
        }
    ]
]

And to deserialize this you can use List< List< TVID > > such as:

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();
            }
        }
    }
}

Looking at the JSON I think a single array would do the same job but if you can't change the JSON the above changes should make it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM