Yaser
Yaser

Reputation: 93

Cannot deserialize the current JSON object wirth whis json

{
    "token": "d10e",
    "@version": "1",
    "key": "30a8-e2dc2c6c7a5e",
    "result": [
        ["name :Customer10", "description :Hesco"],
        ["name :Customer16", "description :IoTLab"],
        ["name :Customer32", "description :Abdevand"],
        ["name :Customer20", "description :Jahad Daneshgahi KNU"],
        ["name :Customer8", "description :Babinab"],
        ["name :Customer4", "description :ISA"],
        ["name :ParsIoT", "description :customer created on 2018-01-16T05:45:05.939 (BSS Time)"],
        ["name :Customer18", "description :Customer18"]
    ]
}

how can i deserialize to json array??

C# return error and can not deserialize this json !!!

var resp = JsonConvert.DeserializeObject<List<Organization>>(response);

This is the organization class , and how can i list result object

public class Organization
{
    [JsonProperty("token")]
    public string token { get; set; }

    [JsonProperty("@version")]
    public string version { get; set; }

    [JsonProperty("@timestamp")]
    public string timestamp { get; set; }

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

    [JsonProperty("result")]
    public Result[] result { get; set; }
}

And this is the Result class:

public class Result
{
    [JsonProperty("name")]
    public string name { get; set; }

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

Upvotes: 1

Views: 61

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500155

You have two problems:

  • You're trying to deserialize to a List<Organization>, but your JSON only represents a single Organization.
  • The result property is an array of arrays of strings - the JSON doesn't contain any "name" or "description" properties, just values that look like "name :Customer20" etc.

Here's a complete example that does work:

using System;
using System.IO;
using Newtonsoft.Json;

public class Organization
{
    [JsonProperty("token")]
    public string Token { get; set; }

    [JsonProperty("@version")]
    public string Version { get; set; }

    [JsonProperty("key")]
    public string Key { get; set; }

    [JsonProperty("result")]
    public string[][] Results { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        string json = File.ReadAllText("test.json");
        var org = JsonConvert.DeserializeObject<Organization>(json);
        Console.WriteLine($"Token: {org.Token}");
        Console.WriteLine($"Version: {org.Version}");
        Console.WriteLine($"Key: {org.Key}");
        Console.WriteLine("Result entries:");
        foreach (string[] entry in org.Results)
        {
            Console.WriteLine(string.Join(", ", entry));
        }
    }
}

But now you'll need to parse each entry in the result array individually. (If you can modify the format of the JSON, it would be better if each entry were an object, but the code above deals with the JSON as you've presented it.)

Upvotes: 3

Related Questions