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 を使用してすべてのプロパティとすべてのデータにアクセスし、必要に応じて結果に変換することもできます。

この Web サイトを使用して、JSON http://json2csharp.com/からクラスを作成できます。

于 2013-05-08T18:55:11.240 に答える