c# - 使用正则表达式或 JSON 提取数据

标签 c# json regex

我有以下 json 格式的数据。

{
  "predictions": [
    {
      "prediction": "76A Fonthill Road, Aberdeen, Aberdeenshire, AB11 6UL",
      "refs": "52833271",
      "complete": false
    },
    {
      "prediction": "76B Fonthill Road, Aberdeen, Aberdeenshire, AB11 6UL",
      "refs": "52833272",
      "complete": false
    }
  ],
  "status": "Ok"
}

我试过使用 Json.net 但我无法获得我需要的数据 我想要地址

76A Fonthill Road, Aberdeen, Aberdeenshire, AB11 6UL

我也试过用

regex Regex Exp = new Regex("\"prediction\":\"(.*),\"refs\""); 

但它匹配

76A Fonthill Road, Aberdeen, Aberdeenshire, AB11 6UL","refs":"52833271","complete":false},{"prediction":"76B Fonthill Road, Aberdeen, Aberdeenshire, AB11 6UL","refs"

它在 PHP 中尝试使用 json_decode() 并使用 Regex 我可以正确提取所有数据。

76A Fonthill Road, Aberdeen, Aberdeenshire, AB11 6UL \n 76B Fonthill Road, Aberdeen, Aberdeenshire, AB11 6UL \n*

我需要一个 C# 的解决方案。

最佳答案

创建一组与您的 JSON 匹配的类。

 public class Predictions
{
    public string Prediction { get; set; }
    public string Refs { get; set; }
    public bool Complete { get; set; }
}

public class PredictionsList
{
    public List<Predictions> Predictions { get; set; }
    public string Status { get; set; }
}

然后使用JsonConvert反序列化

  var dataDictionary = JsonConvert.DeserializeObject<PredictionsList>(json);

关于c# - 使用正则表达式或 JSON 提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41132002/

相关文章:

c# - 使用函数可选参数默认值

javascript - 如何从 html 中更新 json?

java - 在 Java 中使用正则表达式进行部分精确匹配

jquery - 在我的例子中,如何从 Json Stringify 读取值?

java - Android RestTemplate json反序列化

regex - 为什么 Perl 正则表达式中的 * 似乎并不贪婪?

c# - 字符串解析,提取数字和字母

c# - 如何自动生成WPF Event -> IObservable的扩展方法库

javascript - 在后面的代码中获取内部 html 值(C#)

c# - 相同的代码,C# 和 C++ 中的不同输出