我的api得到像这样的json数据
Office "دكتور نيوترشن"
officename "11111111"
address_user "سيتي مول"
profile_photo "profile_photo.png"
cover_photo "cover_photo.jpg"
agentarea "القطيف"
offertext "قريبا سوف توضح المصومات"
websiteurl "albatool-hdo"
在xamarin android我正在创建这个类
public class Galeri
{
public Galeri()
{
}
public string Office { get; set; }
public string officename { get; set; }
public string address_user { get; set; }
public string profile_photo { get; set; }
public string cover_photo { get; set; }
public string agentarea { get; set; }
public string offertext { get; set; }
public string websiteurl { get; set; }
public string membertype { get; set; }
}
我也正在使用此代码反序列化json
RunOnUiThread(() =>
{
itemGaleri = JsonConvert.DeserializeObject<List<Galeri>>(e.Result);
CustomListAdapter adapter = new CustomListAdapter(this, itemGaleri);
DaftarGaleri.Adapter = adapter;
progress.Visibility = ViewStates.Gone;
DaftarGaleri.ItemClick += DaftarGaleri_ItemClick;
}
);
但是我收到了错误
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type '' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
答案 0 :(得分:0)
下次当您必须处理JSON并且想要快速生成C#模型时,请使用https://app.quicktype.io之类的服务。现在只需为您的属性提供逻辑名称,例如Galeri
而不是Welcome
。
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var data = Welcome.FromJson(jsonString);
namespace QuickType
{
using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
public partial class Welcome
{
[JsonProperty("Office")]
public string Office { get; set; }
[JsonProperty("officename")]
public string Officename { get; set; }
[JsonProperty("address_user")]
public string AddressUser { get; set; }
[JsonProperty("profile_photo")]
public string ProfilePhoto { get; set; }
[JsonProperty("cover_photo")]
public string CoverPhoto { get; set; }
[JsonProperty("agentarea")]
public string Agentarea { get; set; }
[JsonProperty("offertext")]
public string Offertext { get; set; }
[JsonProperty("websiteurl")]
public string Websiteurl { get; set; }
[JsonProperty("membertype")]
public string Membertype { get; set; }
}
public partial class Welcome
{
public static Welcome[] FromJson(string json) => JsonConvert.DeserializeObject<Welcome[]>(json, Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome[] self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
}