现在我有了这个 JSON:
[
"bekasi",
"bekasi barat",
"bekasi selatan",
"bekasi timur",
"bekasi utara",
"serang bekasi"
]
我不知道如何制作解析器类。我尝试了在线 json to c# 类生成器,但无济于事。所以我尝试了这个:
[DataContract]
public class kota
{
[DataMember]
public string kotanya { get; set; }
}
还有这个
public static kota eks; // I also tried public static kota[] eks;
public void mulai()
{
string eksp = "http://www.ongkoskirim.com/api/0.2/?id=OAL66afd139a386fee6dc5a5597abd7daba&q=city&s=bek";
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(eksp), UriKind.Absolute);
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
var ser = new DataContractJsonSerializer(typeof(kota)); //and I have tried typeof(IEnumerable<kota>)
System.Diagnostics.Debug.WriteLine("sini");
eks = (kota)ser.ReadObject(e.Result); //I also tried (kota[])ser.ReadObject(e.Result);
System.Diagnostics.Debug.WriteLine("sana");
List<string> temp = new List<string>();
temp.Add("Semua");
System.Diagnostics.Debug.WriteLine("list");
for (int i = 0; i < 3; i++)
{
System.Diagnostics.Debug.WriteLine(eks.kotanya[i]);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
}
但我总是得到 invalidCastException。谁能帮我 ?