Problem: display JSON data but some of the parent data is integer

{
    "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"
  }
}

I need to get the child data of "1" parent data. I was trying to deserialize this json data but I'm having a problem because the parent data is a number.

UPDATE: How will I get the parent data integer if they are 2 or more? because, on my API, the json data has different number of records depends on customer's order list.

1 answer

  • answered 2018-11-08 06:23 D-Shih

    You can try to use json.net to serializat.

    If the key contains a special word you can try to use JsonProperty to mark binding key from your 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; }
    }
    

    Then you can use like

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

    Note

    There are two way can create model easily.

    • You can use Web Essentials in Visual Studio, use Edit > Paste special > paste JSON as a class, you can easier to know the relation between Json and model.

    • If you can't use Web Essentials you can instead of use https://app.quicktype.io/?l=csharp online JSON to Model class.

    You can try to use those models to carry your JSON Format.