【问题标题】:How to get list of objects from json string asp.net mvc Newtonsoft.json如何从 json 字符串 asp.net mvc Newtonsoft.json 获取对象列表
【发布时间】:2019-10-15 12:17:31
【问题描述】:

我正在尝试将 JSON 字符串转换为对象列表,但出现错误

无法将当前 JSON 对象反序列化为 List 类型,因为该类型需要 JSON 数组才能正确反序列化。

我检索了 json,它看起来像这样

     {
    "totalCount": 4,
    "items": [
        {
            "Tarih": "10-10-2019",
            "TP_DK_SAR_A": "1.5538",
            "TP_DK_SAR_S": "1.5566",
            "TP_DK_EUR_A": "6.3974",
            "TP_DK_EUR_S": "6.4089",
            "TP_DK_CHF_A": "5.8504",
            "TP_DK_GBP_A": "7.1119",
            "TP_DK_JPY_A": "5.4183",
            "UNIXTIME": {
                "$numberLong": "1570658400"
            }
        },
        {
            "Tarih": "11-10-2019",
            "TP_DK_SAR_A": "1.5651",
            "TP_DK_SAR_S": "1.568",
            "TP_DK_EUR_A": "6.4694",
            "TP_DK_EUR_S": "6.481",
            "TP_DK_CHF_A": "5.9019",
            "TP_DK_GBP_A": "7.1727",
            "TP_DK_JPY_A": "5.4515",
            "UNIXTIME": {
                "$numberLong": "1570744800"
            }
        },
        {
            "Tarih": "12-10-2019",
            "TP_DK_SAR_A": null,
            "TP_DK_SAR_S": null,
            "TP_DK_EUR_A": null,
            "TP_DK_EUR_S": null,
            "TP_DK_CHF_A": null,
            "TP_DK_GBP_A": null,
            "TP_DK_JPY_A": null,
            "UNIXTIME": {
                "$numberLong": "1570831200"
            }
        },
        {
            "Tarih": "13-10-2019",
            "TP_DK_SAR_A": null,
            "TP_DK_SAR_S": null,
            "TP_DK_EUR_A": null,
            "TP_DK_EUR_S": null,
            "TP_DK_CHF_A": null,
            "TP_DK_GBP_A": null,
            "TP_DK_JPY_A": null,
            "UNIXTIME": {
                "$numberLong": "1570917600"
            }
        }
    ]
}

【问题讨论】:

    标签: asp.net model-view-controller


    【解决方案1】:

    您好,我解决了这个问题并使用了两种不同的方法;

    1-)
         ExchangeRateGetViewModel CompanyInfoData = Newtonsoft.Json.JsonConvert.DeserializeObject<ExchangeRateGetViewModel>(jsonResultString);
    
    2-)
    var o = JsonConvert.DeserializeObject<JObject>(jsonResultString);
                var h = o.Value<JArray>("items")
                    .ToObject<List<ExchangeRateGetListViewModel>>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多