0

This is my code

""{\n  \"data\": [\n    {\n      \"listid\": \"\",\n      \"name\": \"\"\n    }\n  ]\n}""

In the above string was coming from server. I want to deserialize the string and get keys from array of objects. I tried using JsonConvert.Deserialization with Dictionary but it throws an exception. I also tried JObject and JArray. I want to get listId, name keys.

2
  • Have you tried the NewtonJsoft library, that seems to the favourite json serialiser at the moment. Commented Aug 19, 2016 at 9:56
  • 1
    @Slicc, JsonConvert is json.net. I just fixed tags. And to OP, please show your code and error if you want us to help you. Commented Aug 19, 2016 at 9:58

3 Answers 3

3

Have you tried to deserialise to your own type? This works for me:

    public class Program
{
    public static void Main(string[] args)
    {
        string deser = "{\n  \"data\": [\n    {\n      \"listid\": \"\",\n      \"name\": \"\"\n    }\n  ]\n}";

        var obj = JsonConvert.DeserializeObject<MyCollection>(deser);


    }
}


public class MyType
{
    [JsonProperty("listid")]
    public string ListId { get; set; }

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

public class MyCollection
{
    [JsonProperty("data")]
    public List<MyType> Data { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I got Solution, following code works fine

string json = "{\n  \"data\": [\n    {\n      \"listid\": \"\",\n      \"name\": \"\"\n    }\n  ]\n}";
        var jss = new JavaScriptSerializer();
        List<string> li = new List<string>();
        dynamic jsondata = jss.Deserialize<dynamic>(json);
        foreach (string key in jsondata["data"][0].Keys)
        {
            li.Add(key);
        }

Comments

2

You can use Newtonsoft.Json library in C#

You need to create the corresponding Classes with the matching property names as that of the json array in C# in order to deserialize the json string to C# objects.

See the following code.

namespace StackOverflow.Test
{
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            var json = "{\n  \"data\": [\n    {\n      \"listid\": \"123\",\n      \"name\": \"Name\"\n    }\n  ]\n}";
            var lists = JsonConvert.DeserializeObject(json, typeof(Lists)) as Lists;
            var list = lists.Data.FirstOrDefault();

            Console.WriteLine("Name: " + list.Name);
            Console.WriteLine("List Id: " + list.ListId);

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }

    class Lists
    {
        public List<Info> Data { get; set; }
    }

    class Info
    {
        public string ListId { get; set; }

        public string Name { get; set; }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.