简体   繁体   中英

JSON Deserialization C#

I'm trying to parse out some information I've retrieved from RottenTomatoes that's in a JSON format

{
    "cast": [
        {
            "id": "162655641",
            "name": "Tom Hanks",
            "characters": [
                "Woody"
            ]
        },
        {
            "id": "162655909",
            "name": "Tim Allen",
            "characters": [
                "Buzz Lightyear"
            ]
        },
        {
            "id": "162655020",
            "name": "Joan Cusack",
            "characters": [
                "Jessie the Cowgirl"
            ]
        },
        {
            "id": "162672460",
            "name": "Ned Beatty",
            "characters": [
                "Lots-o'-Huggin' Bear",
                "Lotso"
            ]
        },
        {
            "id": "162657445",
            "name": "Richard Kind",
            "characters": [
                "Bookworm"
            ]
        },
        {
            "id": "162654813",
            "name": "Erik von Detten",
            "characters": [
                "Sid"
            ]
        },
        {
            "id": "770713272",
            "name": "James Anthony Cotton",
            "characters": []
        }
    ],
    "links": {
        "rel": "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122.json"
    }
}

I'm just trying to get this code to work but I'm getting an InvalidOperationException and this error "Type 'System.String' is not supported for deserialization of an array."

Here's my code in main

string json = File.ReadAllText("json.txt");

CastInfo castMember = new JavaScriptSerializer().Deserialize<CastInfo>(json);

Here are my classes

public class CastInfo
{
    public List<CustomCastInfo> cast { get; set; }
}
public class CustomCastInfo
{
    public string id { get; set; }
    public string name { get; set; }
    public List<string> characters { get; set; }

}

And advice? And I realize I need to do something about the "links" in the bottom, but even when I delete that it still doesn't work.

I just tried running this with the json you provided and it worked fine.

using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;

namespace JsonDeserialization
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = File.ReadAllText("json.txt");

            CastInfo castMember = new JavaScriptSerializer().Deserialize<CastInfo>(json);
        }
    }

    public class CastInfo
    {
        public List<CustomCastInfo> cast { get; set; }
    }
    public class CustomCastInfo
    {
        public string id { get; set; }
        public string name { get; set; }
        public List<string> characters { get; set; }

    }

}

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