1

json 文字列をリストに逆シリアル化しようとしています。Json 文字列が空ではありません。しかし、逆シリアル化の後、リストは 0 アイテムを返します。

これは私のクラスです。

 [Serializable]
    public class Products : GenericItem
    {
        public List<string> Images { get; set; }
        public double Price { get; set; }
        public double SalePrice { get; set; }

        public bool OnSale
        {
            get { return (SalePrice < Price); }
        }

        public string Description { get; set; }

        public GenericItem Brand { get; set; }

        public List<GenericItem> Shops { get; set; }

        public string ProductCode { get; set; }

        public Colour Colour { get; set; }
    }

    [Serializable]
    public class Colour : GenericItem
    {
        public string Code { get; set; }
    }

    [Serializable]
    public class GenericItem
    {
        public string Name { get; set; }

        public string Permalink { get; set; }
    }

これは私のjson文字列です。

"{\"Products\":[{\"_id\":\"515c151f7be95925d4ee794d\",\"Name\":\"CAT Formation Steel Toe - Boots  BLACK\",\"Description\":\"CAT Formation Steel Toe Boots With heavy-duty hardwearing uppers, high grip rubber outsoles and a steel toe cap, these CAT Formation Steel Toe Boots are perfect for work. CAT Formation Steel Toe Boots are ankle length and fasten with strong laces for a secure fit. The boots are available in black and have a casual multi-panelled trainer-style appearance, as well as CAT branding on the tongue and heel.\",\"ProductCode\":\"D0073\",\"Images\":[\"18000.jpg\",\"18001.jpg\",\"18002.jpg\"],\"Brand\":{\"_id\":\"5142ca0c7be95917acce7755\",\"Name\":\"CAT\",\"Permalink\":\"cat-footwear\"},\"Shops\":[{\"_id\":\"5142ca237be95917acce7999\",\"Name\":\"Mens\",\"Styles\":null,\"Permalink\":\"Mens\"}],\"Permalink\":\"cat-formation-steel-toe-boots-black\",\"Price\":89.99,\"SalePrice\":89.99}]}"

これは、json文字列を逆シリアル化する方法です。

 var jss = new JavaScriptSerializer();
            jss.MaxJsonLength = Int32.MaxValue;
            var results = jss.Deserialize<List<Products>>(json);

リストは常に 0 の結果を返します。

誰かが私を助けてくれませんか。

4

1 に答える 1

3

あなたの JSON はリストではなく、Products プロパティを持つオブジェクト、つまりリストです。

次のようなものに逆シリアル化できます。

public class MyClass
{
    public List<Products> Products { get; set; }
}
...

jss.Deserialize<MyClass>(json);
于 2013-04-03T15:01:03.027 に答える