1

我正在尝试解析从 RottenTomatoes 检索到的一些 JSON 格式的信息

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

我只是想让这段代码工作,但我得到一个 InvalidOperationException 并且这个错误“类型'System.String'不支持数组的反序列化。”

这是我的主要代码

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

}

和建议?而且我意识到我需要对底部的“链接”做一些事情,但即使我删除它仍然不起作用。

4

1 回答 1

1

我刚刚尝试使用您提供的 json 运行它,并且效果很好。

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

    }

}
于 2013-03-02T06:44:56.180 回答