繁体   English   中英

如何在 C# 中解析 JSON

[英]How to parse JSON in C#

我有一个奇怪的 JSON 字符串,需要在 C# 中进行解析。 我该怎么做。 我尝试将其解析为Dictionary但失败了。 我可以以某种方式创建一个哈希图吗?

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

JSON 字符串在这里

我正在使用 API 获取这些数据。 这是我收到的错误。

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'System.Collections.Generic.Dictionary`2[System.String,System.String]' 因为该类型需要 JSON 对象(例如 {"name ":"value"}) 正确反序列化。

要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollectionIList ),如List<T>可以从 JSON 数组反序列化。 JsonArrayAttribute也可以添加到类型以强制它从 JSON 数组反序列化。

您的 JSON 数据的形状不适合Dictionary<string,string>因为在其顶层,它不是字典。 它是一个对象数组。

您需要编写一个与数据形状匹配的自定义类型 ( class ),然后反序列化为此类类型的集合。

所以,广义上讲(使用未经测试的代码......)

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

然后

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

这是因为您的 JSON 不代表Dictionary<string, string> recruitersMapjobsMap都嵌套object S或集合,而不是string S上。

你可以创建一个 POCO 类

    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; }
    }

并将 json 反序列化为该类的对象,如下所示:

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

我看到的是,您指出的 JSON 具有常规结构,您应该为其创建一个包装模型,请参见下面的示例或在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; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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