今、私はこの JSON を持っています:
[
"bekasi",
"bekasi barat",
"bekasi selatan",
"bekasi timur",
"bekasi utara",
"serang bekasi"
]
パーサークラスの作り方がわかりません。オンライン json を 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を取得しています。誰でも私を助けることができますか?