首页 > 解决方案 > 问题:显示 JSON 数据但某些父数据是整数

问题描述

{
    "recsonpage": "1",
    "recsindb": "1",
    "1": {
        "orders.orderid": "84344969",
        "entity.customerid": "19421067",
        "entity.entityid": "84344969",
        "orders.autorenew": "false",
        "orders.endtime": "1572414460",
        "orders.resellerlock": "false",
        "orders.timestamp": "2018-10-30 05:47:41.791511+00",
        "orders.customerlock": "true",
        "entity.entitytypeid": "87",
        "entity.currentstatus": "Active",
        "entitytype.entitytypekey": "dotco",
        "orders.transferlock": "true",
        "orders.creationtime": "1540878460",
        "orders.privacyprotection": "false",
        "entitytype.entitytypename": ".CO Domain Name",
        "orders.creationdt": "1540878459",
        "entity.description": "testlbapi.co"
    },
   "2": {
       "orders.orderid": "84421837",
       "entity.customerid": "19442287",
       "entity.entityid": "84421837",
       "orders.autorenew": "true",
       "orders.endtime": "1573084799",
       "orders.resellerlock": "false",
       "orders.timestamp": "2018-11-06 17:06:25.269568+00",
       "orders.customerlock": "false",
       "entity.entitytypeid": "450",
       "entity.currentstatus": "Active",
       "entitytype.entitytypekey": "dotspace",
       "orders.transferlock": "false",
       "orders.creationtime": "1541523983",
       "orders.privacyprotection": "false",
       "entitytype.entitytypename": ".SPACE Domain Names",
       "orders.creationdt": "1541435012",
       "entity.description": "thairaksaa.space"
   },
  "3": {
       "orders.orderid": "84421838",
       "entity.customerid": "19442287",
       "entity.entityid": "84421838",
       "orders.autorenew": "true",
       "orders.endtime": "1604682389",
       "orders.resellerlock": "false",
       "orders.timestamp": "2018-11-06 17:06:30.199466+00",
       "orders.customerlock": "true",
       "entity.entitytypeid": "3",
       "entity.currentstatus": "Active",
       "entitytype.entitytypekey": "domcno",
       "orders.transferlock": "true",
       "orders.creationtime": "1541523989",
       "orders.privacyprotection": "false",
       "entitytype.entitytypename": ".COM Domain Name",
       "orders.creationdt": "1541435014",
       "entity.description": "thairaksaa.com"
  }
}

我需要获取“1”父数据的子数据。我试图反序列化这个 json 数据,但我遇到了问题,因为父数据是一个数字。

更新:如果父数据整数为 2 或更多,我将如何获取它们?因为,在我的 API 上,json 数据的记录数取决于客户的订单列表。

标签: c#json

解决方案


您可以尝试使用json.net进行序列化。

如果键包含特殊字词,您可以尝试使用JsonProperty从您的JSON.

public partial class JsonModel
{
    [JsonProperty("1")]
    public Dictionary<string, string> obj1 { get; set; }

    [JsonProperty("recsonpage")]
    public string Recsonpage { get; set; }

    [JsonProperty("recsindb")]
    public string Recsindb { get; set; }
}

然后你可以使用喜欢

var MyDetails = JsonConvert.DeserializeObject<JsonModel>(json);

笔记

有两种方法可以轻松创建模型。

  • 您可以在 Visual Studio 中使用 Web Essentials,使用 Edit > Paste special > paste JSON as a class,您可以更容易地了解 Json 和模型之间的关系。

  • 如果您不能使用 Web Essentials,您可以使用https://app.quicktype.io/?l=csharp online JSON to Model 类来代替。

您可以尝试使用这些模型来承载您的 JSON 格式。


推荐阅读