第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Json.net反序列化JSON對象數(shù)組

使用Json.net反序列化JSON對象數(shù)組

月關(guān)寶盒 2019-08-27 17:24:34
使用Json.net反序列化JSON對象數(shù)組我試圖使用一個API,使用以下示例結(jié)構(gòu)為他們返回的json[    {       "customer":{          "first_name":"Test",          "last_name":"Account",          "email":"test1@example.com",          "organization":"",          "reference":null,          "id":3545134,          "created_at":"2013-08-06T15:51:15-04:00",          "updated_at":"2013-08-06T15:51:15-04:00",          "address":"",          "address_2":"",          "city":"",          "state":"",          "zip":"",          "country":"",          "phone":""       }    },    {       "customer":{          "first_name":"Test",          "last_name":"Account2",          "email":"test2@example.com",          "organization":"",          "reference":null,          "id":3570462,          "created_at":"2013-08-12T11:54:58-04:00",          "updated_at":"2013-08-12T11:54:58-04:00",          "address":"",          "address_2":"",          "city":"",          "state":"",          "zip":"",          "country":"",          "phone":""       }    }]JSON.net可以使用類似以下結(jié)構(gòu)的東西{     "customer": {         ["field1" : "value", etc...],         ["field1" : "value", etc...],     }}但我無法弄清楚如何讓它對提供的結(jié)構(gòu)感到滿意。使用默認(rèn)的JsonConvert.DeserializeObject(content)會生成正確的Customer數(shù),但所有數(shù)據(jù)都為null。在CustomerList(下面)執(zhí)行某些操作會導(dǎo)致“無法反序列化當(dāng)前JSON數(shù)組”異常public class CustomerList{     public List<Customer> customer { get; set; }}思考?
查看完整描述

3 回答

?
慕村225694

TA貢獻1880條經(jīng)驗 獲得超4個贊

您可以創(chuàng)建一個新模型來反序列化您的Json CustomerJson

public class CustomerJson{
    [JsonProperty("customer")]
    public Customer Customer { get; set; }}public class Customer{
    [JsonProperty("first_name")]
    public string Firstname { get; set; }

    [JsonProperty("last_name")]
    public string Lastname { get; set; }

    ...}

你可以輕松地反序列化你的json:

JsonConvert.DeserializeObject<List<CustomerJson>>(json);

希望能幫助到你 !


查看完整回答
反對 回復(fù) 2019-08-27
?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

使用已接受的答案,你必須使用Customers[i].customer,訪問每個記錄,你需要一個額外的CustomerJson類,這有點煩人。如果您不想這樣做,可以使用以下內(nèi)容:

public class CustomerList{
    [JsonConverter(typeof(MyListConverter))]
    public List<Customer> customer { get; set; }}

請注意,我使用的是a List<>,而不是數(shù)組。現(xiàn)在創(chuàng)建以下類:

class MyListConverter : JsonConverter{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var token = JToken.Load(reader);
        var list = Activator.CreateInstance(objectType) as System.Collections.IList;
        var itemType = objectType.GenericTypeArguments[0];
        foreach (var child in token.Values())
        {
            var childToken = child.Children().First();
            var newObject = Activator.CreateInstance(itemType);
            serializer.Populate(childToken.CreateReader(), newObject);
            list.Add(newObject);
        }
        return list;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType.IsGenericType && (objectType.GetGenericTypeDefinition() == typeof(List<>));
    }
    public override bool CanWrite => false;
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();}


查看完整回答
反對 回復(fù) 2019-08-27
?
收到一只叮咚

TA貢獻1821條經(jīng)驗 獲得超5個贊

對于那些不想創(chuàng)建任何模型的人,請使用以下代碼:

var result = JsonConvert.DeserializeObject<
  List<Dictionary<string, 
    Dictionary<string, string>>>>(content);


查看完整回答
反對 回復(fù) 2019-08-27
  • 3 回答
  • 0 關(guān)注
  • 1254 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號