0

I've searched far and beyond but cannot find a good solution for this problem. I have a data set that looks like this(Only strings and null-values):

"rows": [
    [
        "string1",
        "string2",
        null,
        "string4"

    ],
    [
        "string1",
        null,
        "string3",
        null



    ],etc...

I'm getting this data from an api using RestSharp and want to shuffle it in to my database but I'm stuck at how to convert the data to POCOS where an array nested in 'rows' resembles an object, a flat one at that with only string properties.

I have no code as of this moment since nothing is even close to working...

Any fancy suggestions on how to accomplish this? Cheers!

Kasper P
  • 710
  • 2
  • 9
  • 14
  • Something like this? [JSON deserialization - Map array indices to properties with JSON.NET](https://stackoverflow.com/questions/36757412). That answer uses Json.NET. To use Json.NET in RestSharp, see [RestSharp serialization to JSON, object is not using SerializeAs attribute as expected](https://stackoverflow.com/questions/21633649). – dbc Jun 08 '16 at 09:09
  • @dbc: Thank you, thats what I was looking for. – Kasper P Jun 08 '16 at 14:27

3 Answers3

1

Probably the best and safest way is to write it manualy. If you are using vs2015 you can also copy the json and paste it as class.

You can find this option here: enter image description here

Another option is to use a website like http://json2csharp.com/ here you can paste the JSON and this will also generate classes for your JSON. Keep in mind that these generator are not 100% accurate so you might have to change some properties your self. enter image description here

for you example this would look something like this:

public class MyClass
{
   public List<string> rows { get; set; }
}

or with cells

public class RootObject
{
    public List<List<string>> rows { get; set; }
}

Ones we got the model we can use a lib like JSON.net to deserialize our JSON into our model. On the website is also some extra documentation on how to use JSON.net

var model = JsonConvert.DeserializeObject<MyClass>(json); //json being the json string.

Test:

var c = new MyClass()
            {
                rows = new List<string>()
                {
                    "string1",
                    "string2",
                    "string3"
                }

            };
            Console.WriteLine(JsonConvert.SerializeObject(c));
            Console.Read();

Result:

{
    "rows": [
        "string1",
        "string2",
        "string3"
    ]
}

With cell list

Test:

static void Main(string[] args)
        {
            var c = new MyClass()
            {
                rows = new List<List<string>>()
                {
                    new List<string>() {"string1","string2","string3" }
                }

            };
            Console.WriteLine(JsonConvert.SerializeObject(c));
            Console.Read();

        }

        public class MyClass
        {
            public List<List<string>> rows { get; set; }
        }

Result:

{
    "rows": [
        [
            "string1",
            "string2",
            "string3"
        ]
    ]
}
Florian Schaal
  • 2,586
  • 3
  • 39
  • 59
0

You should at least have the input you're receiving from that API, right? If it's a string, you can use Newtonsoft to deserialize that JSON into an object:

var obj = JsonConvert.DeserializeObject(jsonInput)

If you know that the input is always going to be of the same type, you can map that to a class like the following, to benefit from the strongly type advantages.

public class Poco
    {
        public string[] Cell { get; set; }
    }
Alexandru Marculescu
  • 5,569
  • 6
  • 34
  • 50
0

As you are using RestSharp you should just need to create a class and pass that to the deserializer as its target, e.g.:

public class Poco 
{
    public List<List<string>> rows { get; set; }
}

Which can then be used as so:

var result = client.Execute<Poco>(request);
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114