0

我需要反序列化我从图中获得的来自 facebook 的 json 字符串:

{
"id": "1741240583",
"music": {
    "data": [
        {
            "name": "KMN | Kill My Name",
            "id": "168949476496447",
            "created_time": "2013-05-01T07:30:54+0000"
        },
        {
            "name": "Hocus Pocus",
            "id": "174462922710692",
            "created_time": "2013-04-16T17:55:46+0000"
        }
    ]
}
}

我所做的方式是这样的:

    public class Result
    {
        public Music music { get; set; }
    }

    public class Music
    {
        public Data[] data { get; set; }
    }

    public class Data
    {
        public string[] name { get; set; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string teste = "{\"id\": \"723560709\",\"music\": {\"data\": [{\"name\": \"LOKO  ( Life Opium Kill over )\",\"id\": \"129518483779162\",\"created_time\": \"2013-05-07T02:54:39+0000\"},],}}";

        Result soap = JsonConvert.DeserializeObject<Result>(teste);

但它返回此错误:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'GuiaDePresentes.Buscape.Buscape_Controle+Data' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'music.data', line 1, position 38.

我怎样才能使这件事起作用?

PS:是的,我是编程新手 :D

4

1 回答 1

0

我相信您的 JSON 测试字符串格式不正确,我已在http://jsonlint.com/上进行了检查,结果证明无效。

现在,我最近在使用 JSON 时遇到了大问题,但这个网站对我帮助很大 http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

希望能帮助到你 :)

测试示例

using System.Web.Script.Serialization;

public class Datum
    {
        public string name { get; set; }
        public string id { get; set; }
        public string created_time { get; set; }
    }
    public class Paging
    {
        public string next { get; set; }
    }
    public class Music
    {
        public List<Datum> data { get; set; }
        public Paging paging { get; set; }
    }
    public class RootObject
    {
        public string id { get; set; }
        public Music music { get; set; }
    }

 protected void Page_Load(object sender, EventArgs e)
        {
            WebClient wc = new WebClient();

            JavaScriptSerializer json = new JavaScriptSerializer();
            string _token = "BAACEdEose0cBAE8U2tleN51ZBd8g8nBsUPVinB0dYFC2WfsGmbp2x8Vl6ZAynV81IXdZB0PkZCfFscjGjLvg8zSj1FMSi6E7al5sMLUDpZA5ZCR3rWJJjdUYxx3sdLUIaq1BJ8hGjrKPyVW6pEtp96mDmwF2vLnrivcrofvQLO4YNEqpj23Rb5gv7GwRYSbVnplKsg0dHAGS79iAp4ZBZAarwuVpSbNfZBcwZD";
            string _url = "https://graph.facebook.com/YOURID?fields=music.fields(name)&access_token=" + _token;
            string _json = wc.DownloadString(_url);

            RootObject jsonObject= json.Deserialize<RootObject>(_json);

            Response.Write(jsonObject.music.data.First().name);

            Response.Write(_json);

        }

拥有 jsonObject 后,您可以使用 LINQ 访问所有属性和所有数据,如果需要,也可以将其转换为 Result。

你可以使用这个网站从 JSON http://json2csharp.com/创建你的类

于 2013-05-08T18:55:11.240 回答