从 JSON 反序列化为对象列表
Deserialization from JSON into List of Objects
我正在尝试将 json
文件反序列化为对象列表。 JSON
文件要求我使用 UTF8
。我在下面尝试这样做。
FileStream reader1 = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader streamReader1 = new StreamReader(reader1, Encoding.UTF8);
string jsonString1 = streamReader1.ReadToEnd();
byte[] byteArray1 = Encoding.UTF8.GetBytes(jsonString1);
MemoryStream stream1 = new MemoryStream(byteArray1);
DataContractJsonSerializer inputSerializer2;
inputSerializer2 = new DataContractJsonSerializer(typeof(List<Country>));
c = (List<Country>)inputSerializer2.ReadObject(stream1);
stream1.Close();
这实际上是将文件读入列表,但只保存空值和 0。
这个对吗?从外观上看,所有参考文献都在正确的位置。只是我的 classes 上的 toString()
搞砸了吗?
谢谢。
编辑:
下面是我的 class 及其我试图反序列化的成员变量以及 JSON 的片段和我的 toString 覆盖。
[DataContract]
public class Country
{
#region Member Variables
private string m_name;
private string m_capital;
private string m_region;
private string m_subRegion;
private int m_population;
private List<Currency> m_currency = new List<Currency>();
private List<Language> m_language = new List<Language>();
//class 的其余部分
public override string ToString()
{
return "The country name is " + Name + "\n" + "The country capital is " + Capital + "\n" + "The country region is " + Region
+ "\n" + "The country sub region is " + SubRegion + "\n" + "The country population is " + Population + "\n"
+ "The country currency is " + Currencies + "\n" + "The country language is " + Languages;
}
/------------------------------------ ----------------------/
[
{
"currencies": [
{
"code": "AFN",
"name": "Afghan afghani",
"symbol": "؋"
}
],
"languages": [
{
"iso639_1": "ps",
"iso639_2": "pus",
"name": "Pashto",
"nativeName": "پښتو"
},
{
"iso639_1": "uz",
"iso639_2": "uzb",
"name": "Uzbek",
"nativeName": "Oʻzbek"
},
{
"iso639_1": "tk",
"iso639_2": "tuk",
"name": "Turkmen",
"nativeName": "Türkmen"
}
],
"name": "Afghanistan",
"capital": "Kabul",
"region": "Asia",
"subregion": "Southern Asia",
"population": 27657145
}
]
所有变量名称需要匹配json、public
,并且有getter
/setter
.
[DataContract]
public class Country
{
[DataMember]
public string name { get; set; }
[DataMember]
public string capital { get; set; }
[DataMember]
public string region { get; set; }
[DataMember]
public string subRegion { get; set; }
[DataMember]
public int population { get; set; }
[DataMember]
public Currency[] currencies { get; set; }
[DataMember]
public Language[] languages { get; set; }
public override string ToString()
{
return "The country name is " + name + "\n" + "The country capital is " + capital + "\n" + "The country region is " + region
+ "\n" + "The country sub region is " + subRegion + "\n" + "The country population is " + population + "\n"
+ "The country currency is " + currencies + "\n" + "The country language is " + languages;
}
}
[DataContract]
public class Currency
{
[DataMember]
public string code { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string symbol { get; set; }
}
[DataContract]
public class Language
{
[DataMember]
public string iso639_1 { get; set; }
[DataMember]
public string iso639_2 { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string nativeName { get; set; }
}
现在可以使用,但 ToString()
除外,货币和语言将打印为 Currency[]
和 Languages[]
。您需要创建另一个函数来打印 Currency
成员。
此外,最好对 MemoryStream
使用 using
以确保流将被自动处理。
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString1)))
{
// Deserialization from JSON
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(List<Country>));
var countries = (List<Country>)deserializer.ReadObject(ms);
}
我正在尝试将 json
文件反序列化为对象列表。 JSON
文件要求我使用 UTF8
。我在下面尝试这样做。
FileStream reader1 = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader streamReader1 = new StreamReader(reader1, Encoding.UTF8);
string jsonString1 = streamReader1.ReadToEnd();
byte[] byteArray1 = Encoding.UTF8.GetBytes(jsonString1);
MemoryStream stream1 = new MemoryStream(byteArray1);
DataContractJsonSerializer inputSerializer2;
inputSerializer2 = new DataContractJsonSerializer(typeof(List<Country>));
c = (List<Country>)inputSerializer2.ReadObject(stream1);
stream1.Close();
这实际上是将文件读入列表,但只保存空值和 0。
这个对吗?从外观上看,所有参考文献都在正确的位置。只是我的 classes 上的 toString()
搞砸了吗?
谢谢。
编辑: 下面是我的 class 及其我试图反序列化的成员变量以及 JSON 的片段和我的 toString 覆盖。
[DataContract]
public class Country
{
#region Member Variables
private string m_name;
private string m_capital;
private string m_region;
private string m_subRegion;
private int m_population;
private List<Currency> m_currency = new List<Currency>();
private List<Language> m_language = new List<Language>();
//class 的其余部分
public override string ToString()
{
return "The country name is " + Name + "\n" + "The country capital is " + Capital + "\n" + "The country region is " + Region
+ "\n" + "The country sub region is " + SubRegion + "\n" + "The country population is " + Population + "\n"
+ "The country currency is " + Currencies + "\n" + "The country language is " + Languages;
}
/------------------------------------ ----------------------/
[
{
"currencies": [
{
"code": "AFN",
"name": "Afghan afghani",
"symbol": "؋"
}
],
"languages": [
{
"iso639_1": "ps",
"iso639_2": "pus",
"name": "Pashto",
"nativeName": "پښتو"
},
{
"iso639_1": "uz",
"iso639_2": "uzb",
"name": "Uzbek",
"nativeName": "Oʻzbek"
},
{
"iso639_1": "tk",
"iso639_2": "tuk",
"name": "Turkmen",
"nativeName": "Türkmen"
}
],
"name": "Afghanistan",
"capital": "Kabul",
"region": "Asia",
"subregion": "Southern Asia",
"population": 27657145
}
]
所有变量名称需要匹配json、public
,并且有getter
/setter
.
[DataContract]
public class Country
{
[DataMember]
public string name { get; set; }
[DataMember]
public string capital { get; set; }
[DataMember]
public string region { get; set; }
[DataMember]
public string subRegion { get; set; }
[DataMember]
public int population { get; set; }
[DataMember]
public Currency[] currencies { get; set; }
[DataMember]
public Language[] languages { get; set; }
public override string ToString()
{
return "The country name is " + name + "\n" + "The country capital is " + capital + "\n" + "The country region is " + region
+ "\n" + "The country sub region is " + subRegion + "\n" + "The country population is " + population + "\n"
+ "The country currency is " + currencies + "\n" + "The country language is " + languages;
}
}
[DataContract]
public class Currency
{
[DataMember]
public string code { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string symbol { get; set; }
}
[DataContract]
public class Language
{
[DataMember]
public string iso639_1 { get; set; }
[DataMember]
public string iso639_2 { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string nativeName { get; set; }
}
现在可以使用,但 ToString()
除外,货币和语言将打印为 Currency[]
和 Languages[]
。您需要创建另一个函数来打印 Currency
成员。
此外,最好对 MemoryStream
使用 using
以确保流将被自动处理。
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString1)))
{
// Deserialization from JSON
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(List<Country>));
var countries = (List<Country>)deserializer.ReadObject(ms);
}