JSON反序列化为对象

时间:2013-03-08 10:10:22

标签: c# .net json serialization windows-phone-7.1

我正在尝试反序列化以下字符串

{
    "city": [
        {
            "city_id": "31",
            "City_Name": "Bergisch Gladbach",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340553787.JPG"
        },
        {
            "city_id": "33",
            "City_Name": "Bielefeld",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340553877.JPG"
        },
        {
            "city_id": "30",
            "City_Name": "Augsburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340553748.JPG"
        },
        {
            "city_id": "27",
            "City_Name": "Düsseldorf",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1339865688.JPG"
        },
        {
            "city_id": "29",
            "City_Name": "Aachen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340553717.JPG"
        },
        {
            "city_id": "23",
            "City_Name": "Jena",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1339000040.JPG"
        },
        {
            "city_id": "12",
            "City_Name": "Aachen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1336759859.JPG"
        },
        {
            "city_id": "25",
            "City_Name": "Berlin",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1339865665.JPG"
        },
        {
            "city_id": "26",
            "City_Name": "Essen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1339865677.JPG"
        },
        {
            "city_id": "14",
            "City_Name": "Bergisch Gladbach",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1336759880.JPG"
        },
        {
            "city_id": "34",
            "City_Name": "Bochum",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340553912.JPG"
        },
        {
            "city_id": "21",
            "City_Name": "Frankfurt am Main",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1337670208.JPG"
        },
        {
            "city_id": "20",
            "City_Name": "All",
            "City_Image": "http://beta.preis-ente.de/admin/1"
        },
        {
            "city_id": "35",
            "City_Name": "Bonn",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340553958.JPG"
        },
        {
            "city_id": "36",
            "City_Name": "Bottrop",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340553996.JPG"
        },
        {
            "city_id": "37",
            "City_Name": "Braunschweig",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554029.JPG"
        },
        {
            "city_id": "38",
            "City_Name": "Bremen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554080.JPG"
        },
        {
            "city_id": "39",
            "City_Name": "Bremerhaven",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554112.JPG"
        },
        {
            "city_id": "40",
            "City_Name": "Chemnitz",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554147.JPG"
        },
        {
            "city_id": "41",
            "City_Name": "Cottbus",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554189.JPG"
        },
        {
            "city_id": "42",
            "City_Name": "Darmstadt",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554234.JPG"
        },
        {
            "city_id": "43",
            "City_Name": "Dessau ",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554279.JPG"
        },
        {
            "city_id": "44",
            "City_Name": "Dortmund",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554332.JPG"
        },
        {
            "city_id": "45",
            "City_Name": "Duisburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554432.JPG"
        },
        {
            "city_id": "46",
            "City_Name": "Düsseldorf",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554469.JPG"
        },
        {
            "city_id": "47",
            "City_Name": "Erfurt",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554505.JPG"
        },
        {
            "city_id": "48",
            "City_Name": "Erlangen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554546.JPG"
        },
        {
            "city_id": "49",
            "City_Name": "Essen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554585.JPG"
        },
        {
            "city_id": "50",
            "City_Name": "Essen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554585.JPG"
        },
        {
            "city_id": "51",
            "City_Name": "Flensburg ",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554625.JPG"
        },
        {
            "city_id": "52",
            "City_Name": "Frankfurt am Main",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554667.JPG"
        },
        {
            "city_id": "53",
            "City_Name": "Freiburg im Breisgau",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554703.JPG"
        },
        {
            "city_id": "54",
            "City_Name": "Fürth",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554740.JPG"
        },
        {
            "city_id": "55",
            "City_Name": "Gelsenkirchen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554783.JPG"
        },
        {
            "city_id": "56",
            "City_Name": "Gera",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554829.JPG"
        },
        {
            "city_id": "57",
            "City_Name": "Görlitz ",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554864.JPG"
        },
        {
            "city_id": "58",
            "City_Name": "Göttingen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554925.JPG"
        },
        {
            "city_id": "59",
            "City_Name": "Hagen (Westfalen)",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340554969.JPG"
        },
        {
            "city_id": "60",
            "City_Name": "Halle (Saale)",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555012.JPG"
        },
        {
            "city_id": "61",
            "City_Name": "Hamburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555055.JPG"
        },
        {
            "city_id": "62",
            "City_Name": "Hamm",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555097.JPG"
        },
        {
            "city_id": "63",
            "City_Name": "Hannover",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555143.JPG"
        },
        {
            "city_id": "64",
            "City_Name": "Heidelberg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555192.JPG"
        },
        {
            "city_id": "65",
            "City_Name": "Heilbronn",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555550.JPG"
        },
        {
            "city_id": "66",
            "City_Name": "Herne",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555598.JPG"
        },
        {
            "city_id": "67",
            "City_Name": "Hildesheim",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555635.JPG"
        },
        {
            "city_id": "68",
            "City_Name": "Ingolstadt",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555774.JPG"
        },
        {
            "city_id": "69",
            "City_Name": "Jena",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555818.JPG"
        },
        {
            "city_id": "70",
            "City_Name": "Kaiserslautern ",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555863.JPG"
        },
        {
            "city_id": "71",
            "City_Name": "Karlsruhe",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555908.JPG"
        },
        {
            "city_id": "72",
            "City_Name": "Kassel",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555942.JPG"
        },
        {
            "city_id": "73",
            "City_Name": "Kiel",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340555977.JPG"
        },
        {
            "city_id": "74",
            "City_Name": "Koblenz",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556039.JPG"
        },
        {
            "city_id": "75",
            "City_Name": "Köln",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556080.JPG"
        },
        {
            "city_id": "76",
            "City_Name": "Krefeld",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556125.JPG"
        },
        {
            "city_id": "77",
            "City_Name": "Leipzig",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556161.JPG"
        },
        {
            "city_id": "78",
            "City_Name": "Leverkusen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556197.JPG"
        },
        {
            "city_id": "79",
            "City_Name": "Lübeck",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556239.JPG"
        },
        {
            "city_id": "80",
            "City_Name": "Ludwigshafen am Rhein",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556278.JPG"
        },
        {
            "city_id": "81",
            "City_Name": "Magdeburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340556325.JPG"
        },
        {
            "city_id": "82",
            "City_Name": "Mainz",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557473.JPG"
        },
        {
            "city_id": "83",
            "City_Name": "Mannheim",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557533.JPG"
        },
        {
            "city_id": "84",
            "City_Name": "Moers",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557586.JPG"
        },
        {
            "city_id": "85",
            "City_Name": "Mönchengladbach",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557637.JPG"
        },
        {
            "city_id": "86",
            "City_Name": "München",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557760.JPG"
        },
        {
            "city_id": "87",
            "City_Name": "Neuss",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557855.JPG"
        },
        {
            "city_id": "88",
            "City_Name": "Nürnberg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557896.JPG"
        },
        {
            "city_id": "89",
            "City_Name": "Oberhausen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557934.JPG"
        },
        {
            "city_id": "90",
            "City_Name": "Offenbach am Main",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340557984.JPG"
        },
        {
            "city_id": "91",
            "City_Name": "Oldenburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558036.JPG"
        },
        {
            "city_id": "92",
            "City_Name": "Osnabrück",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558082.JPG"
        },
        {
            "city_id": "93",
            "City_Name": "Paderborn",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558119.JPG"
        },
        {
            "city_id": "94",
            "City_Name": "Pforzheim",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558158.JPG"
        },
        {
            "city_id": "95",
            "City_Name": "Plauen ",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558196.JPG"
        },
        {
            "city_id": "96",
            "City_Name": "Potsdam",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558232.JPG"
        },
        {
            "city_id": "97",
            "City_Name": "Recklinghausen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558279.JPG"
        },
        {
            "city_id": "98",
            "City_Name": "Regensburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558320.JPG"
        },
        {
            "city_id": "99",
            "City_Name": "Remscheid",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558369.JPG"
        },
        {
            "city_id": "100",
            "City_Name": "Rostock",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558482.JPG"
        },
        {
            "city_id": "101",
            "City_Name": "Saarbrücken",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558527.JPG"
        },
        {
            "city_id": "102",
            "City_Name": "Salzgitter",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558573.JPG"
        },
        {
            "city_id": "103",
            "City_Name": "Schwerin ",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558621.JPG"
        },
        {
            "city_id": "104",
            "City_Name": "Siegen",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558671.JPG"
        },
        {
            "city_id": "105",
            "City_Name": "Solingen",
            "City_Image": "files/city/1340558822.jpg"
        },
        {
            "city_id": "106",
            "City_Name": "Stuttgart",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558759.JPG"
        },
        {
            "city_id": "107",
            "City_Name": "Trier",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558803.JPG"
        },
        {
            "city_id": "108",
            "City_Name": "Ulm",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558854.JPG"
        },
        {
            "city_id": "109",
            "City_Name": "Wiesbaden",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558898.JPG"
        },
        {
            "city_id": "110",
            "City_Name": "Wilhelmshaven ",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558939.JPG"
        },
        {
            "city_id": "111",
            "City_Name": "Witten",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340558974.JPG"
        },
        {
            "city_id": "112",
            "City_Name": "Wolfsburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340559036.JPG"
        },
        {
            "city_id": "113",
            "City_Name": "Wuppertal",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340559075.JPG"
        },
        {
            "city_id": "114",
            "City_Name": "Würzburg",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340559126.JPG"
        },
        {
            "city_id": "115",
            "City_Name": "Zwickau",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340559178.JPG"
        },
        {
            "city_id": "116",
            "City_Name": "Dresden",
            "City_Image": "http://beta.preis-ente.de/admin/files/city/1340615331.JPG"
        }
    ],
    "success": "true"
}

使用以下代码:

public static List<T> Deserialize<T>(string json)
        {
            List<T> obj = new List<T>();
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            obj = (List<T>)serializer.ReadObject(ms);
            ms.Close();
            ms.Dispose();
            return obj;
        }

[DataContract]
    public class CityList
    {
        public CityList()
        {
            this.city_id = string.Empty;
            this.City_Image = string.Empty;
            this.City_Name = string.Empty;
        }

        #region city id
         [DataMember]
        public string city_id { get; set; }
        #endregion

        #region City Name
        [DataMember]
        public string City_Name { get; set; }
        #endregion

        #region City Image
         [DataMember]
        public string City_Image { get; set; }
        #endregion
    }

当我调用函数Deserialize&gt;(jsonString)时 我怎样才能解决这个问题? 我收到了以下错误:

  Error: InvalidCastException was Unhandled

  at ListPickerSample.Common.Utility.Deserialize[T](String json)
   at ListPickerSample.MainPage.client_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Delegate.DynamicInvokeOne(Object[] args)
   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
   at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
   at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
   at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
   at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

1 个答案:

答案 0 :(得分:1)

是的,您的City对象与您的JSON字符串不匹配。

这是一个有效的版本:

public class CityList
{
    public List<City> city { get; set; }
    public bool success { get; set; }
}

public class City
{
    public string city_id { get; set; }
    public string City_Name { get; set; }
    public string City_Image { get; set; }
}

public class Program
{
    private static void Main(string[] args)
    {
        var input = YOUR_STRING;

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CityList));
        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(input)))
        {
            var zz = serializer.ReadObject(ms);
        }
    }
}