【问题标题】:simple json retrieval简单的json检索
【发布时间】:2018-12-31 12:33:10
【问题描述】:

我知道这是一个旧栗子,但我想这样做 而不 导入 newton-soft 或 json.net

我知道这应该可行

这是 json:

{ "do": "Thing", "with": "abc" }

字面意思就是这样。我需要把它带入 c# 领域

这是我目前所拥有的

var json = wc.DownloadString("url");
Console.WriteLine("GOT >> " + json); //says GOT >> { "do": "Thing", "with": "abc" }
var sJson = new JavaScriptSerializer();
var data = sJson.Deserialize<Dictionary<string, string>[]>(json); //crashes with No parameterless constructor defined for type of 'System.Collections.Generic.Dictionary

从我的单行 json 中获取 data["do"]data["with"] 的最精简、最不臃肿的方式是什么?它只会返回一件事......如果我必须串走它,我会的,但它不应该这么难

【问题讨论】:

  • 为什么是Dictionary&lt;string, string&gt;[] 数组?你所拥有的充其量是Dictionary&lt;string, string&gt;
  • 因为“do”和“with”,但是想想我不需要那个,原来里面有一个数组用于with,我把它切碎了一点
  • 我最后是这样做的 Do data = sJson.Deserialize&lt;Do&gt;(json); 其中Do 只是 public string todo = ""; public string with = ""; 似乎仍然非常不理想,但是嘿嘿
  • “做”和“与”什么? :) 您的示例 JSON 中没有数组,因此它对应于 Dictionary&lt;string, string&gt;,“do”和“with”是键,“Thing”和“abc”是它们对应的值。
  • @MrHeelis 检查provided answer

标签: c# json dictionary


【解决方案1】:

您可以为数据创建一个支持类

public class Data {
    public string do { get; set; }
    public string with { get; set; }
}

然后简单地反序列化

var data = sJson.Deserialize<Data>(json);

如果提供的数据实际上是一个数组,则相应地更新通用返回类型

【讨论】:

    【解决方案2】:

    您的 JSON 中没有数组,只是一个简单的对象,因此可以将其反序列化为单个 Dictionary 实例。只需将Dictionary&lt;string, string&gt;[] 更改为Dictionary&lt;string, string&gt;。像这样:

    var data = sJson.Deserialize<Dictionary<string, string>>(json);
    

    然后您可以像这样访问您的值:

    data["do"] // returns "Thing"
    data["with"] // returns "abc"
    

    【讨论】:

      【解决方案3】:

      数组是问题所在。试试这个(Try it Online!):

      var json = "{ \"do\": \"Thing\", \"with\": \"abc\" }";
      var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
      Console.WriteLine(data["do"]);
      Console.WriteLine(data["with"]);
      

      输出

      Thing
      abc
      

      请注意,我在这里使用的是文档中所写的 Json.NET:

      Json.NET 应该用于序列化和反序列化。为支持 AJAX 的应用程序提供序列化和反序列化功能。

      source

      【讨论】:

      • 作者在他的问题开头写道:我想在不导入newton-soft或json.net的情况下这样做 ;)
      • 哦,我错过了。
      【解决方案4】:

      您可以使用正则表达式:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Text.RegularExpressions;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  string input = "{ \"do\": \"Thing\", \"with\": \"abc\" }";
                  string pattern = "\"(?'key'[^\"]+)\":\\s+\"(?'value'[^\"]+)";
      
                  MatchCollection matches = Regex.Matches(input, pattern);
      
                  Dictionary<string, string> dict = matches.Cast<Match>()
                      .GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
                      .ToDictionary(x => x.Key, y => y.FirstOrDefault());
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 2023-01-02
        • 1970-01-01
        • 2012-01-03
        • 2012-04-18
        • 1970-01-01
        相关资源
        最近更新 更多