Manipulate JSON with numeric headers

1

Good afternoon colleagues, I'm consulting a WS that returns a json like the following.

{
    "data": {
        "1130987": [
            1130987
        ],
        "1230988": [
            1230988
        ],
        "1331027": [
            1331027
        ],
        "1405538": [
            1405538
        ],
        "1508418": [
            1508418
        ],
        "1608432": [
            1608432
        ],
        "1708439": [
            1708439
        ],
        "32008440": [
            32008440
        ],
        "32008456": [
            32008456
        ],
        "1500002134": [
            1500010872
        ]
    },
    "range": {
        "total": 3182,
        "from": 1,
        "to": 10
    }
}

The data varies so I can not create a model to be able to transform the json my question is. how can I get the numerical values that come between [].

I appreciate your time. Greetings.

    
asked by Fredo 28.12.2018 в 19:04
source

2 answers

0

You can solve it if you use the correct class to map the json

using System;
using Newtonsoft.Json;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string json = @"{
                        'data': {
                            '1130987': [
                                1130987
                            ],
                            '1230988': [
                                1230988
                            ],
                            '1331027': [
                                1331027
                            ],
                            '1405538': [
                                1405538
                            ],
                            '1508418': [
                                1508418
                            ],
                            '1608432': [
                                1608432
                            ],
                            '1708439': [
                                1708439
                            ],
                            '32008440': [
                                32008440
                            ],
                            '32008456': [
                                32008456
                            ],
                            '1500002134': [
                                1500010872
                            ]
                        },
                        'range': {
                            'total': 3182,
                            'from': 1,
                            'to': 10
                        }
                    }";

        var data = JsonConvert.DeserializeObject<RootObject>(json);

        foreach ( var item in data.DataItems)
        {
            Console.WriteLine(item.Value[0]);
        }
    }
}

public class RootObject
{
    [JsonProperty(PropertyName = "data")]
    public Dictionary<string,int[]> DataItems { get; set; }

    public Range range { get; set; }
}

public class Range
{
    public int total { get; set; }
    public int from { get; set; }
    public int to { get; set; }
}

The magic is in defining the property as Dictionary<>

 [JsonProperty(PropertyName = "data")]
 public Dictionary<string,int[]> DataItems { get; set; }

in this way the dynamic properties map with this where the dictionary value will be a numerical array, taking only one of these

    
answered by 28.12.2018 / 20:31
source
0

I think you can use this structure:

class Info
{
    public JObject Data { get; set; }
    public Range Range { get; set; }
}

class Range
{
    public int Total { get; set; }
    public int From { get; set; }
    public int To { get; set; }
}

This would be the way I recommend to deserialize:

JsonSerializerSettings settings = new JsonSerializerSettings();
 settings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

var value = JsonConvert.DeserializeObject<Info>(jsonString, settings);

var items = value?.Data?.Values()
    .Select(x => x.First.Value<long>());

Console.WriteLine(items);
    
answered by 28.12.2018 в 20:23