简体   繁体   中英

How to parse JSON in C#

I have a weird JSON string which needs to be parsed in C#. How do I do it. I tried parsing it as a Dictionary but it failed. Can I somehow create a hashmap?

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);

The JSON string is here .

I am getting this data using a API. This is the error that i am receiving.

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection , IList ) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

The shape of your JSON data won't fit in a Dictionary<string,string> because at its top level, it's not a dictionary. It's an array of objects.

You need to write a custom type ( class ) that matches the shape of your data, then deserialize to a collection of such types.

So, broadly speaking (with untested code...)

public class SomeType
{
    public string notificationId{ get; set; }
    //etc
    public Dictionary<string,string> recruitersMap{ get; set; }
    //etc
}

then

JsonConvert.Deserialize<List<SomeType>>(someJson)

this is because your JSON does not represent a Dictionary<string, string> . recruitersMap and jobsMap are both nested object s or collections, not string s.

you could create a POCO class

    public class ApiEndPoint // you will find a better name, I'm sure
    {
        public string notificationId { get; set; }
        public string readFlag { get; set; }
        public string importantFlag { get; set; }
        public string subject { get; set; }
        public string folder { get; set; }
        public DateTime creationTime { get; set; }
        public int notificationCount { get; set; }
        public string jobId { get; set; }
        public Dictionary<string, string> recruitersMap { get; set; }
        public Dictionary<string, string> jobsMap { get; set; }
    }

and deserialize the json into a object of that class like this:

JsonConvert.DeserializeObject<List<ApiEndPoint>>(yourJsonString);

What I see is that the JSON you have pointed out has a regular structure and you should create a wrapping model for it see example below or play with it in https://dotnetfiddle.net/TGWTNC .

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


public class Program
{
    public static void Main(string[] args)
    {

        var webClient = new WebClient();
        var json = webClient.DownloadString("https://gist.githubusercontent.com/maskaravivek/33aa0d6556bbb9ecb77a/raw/b815daa55719a754eef5117321e2c0c5621c6a18/gistfile1.txt");
        var notifications = JsonConvert.DeserializeObject<Notification[]>(json);

        Console.WriteLine(notifications.Count());
        Console.ReadLine();
    }
}

//adjust the data types according to your needs
public class Notification
{
    public string notificationId { get; set; }
    public string readFlag { get; set; }
    public string importantFlag { get; set; }
    public string subject { get; set; }
    public string folder { get; set; }
    public string creationTime { get; set; }
    public string notificationCount { get; set; }
    public string jobId { get; set; }
    public Dictionary<string, string> recruitersMap { get; set; }
    public Dictionary<string, string> jobsMap { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM