C # - разобрать и прочитать JSON, который имеет дубликаты ключей - PullRequest
0 голосов
/ 15 ноября 2018

Я пытаюсь выяснить, как я могу проанализировать приведенный ниже JSON и получить из него «текст»: «юрист».Я вижу, что это имеет много разветвлений.т.е. массивы и объекты.Я хочу сделать это в C #.Вот JSON:

{
  "status": "Succeeded",
  "succeeded": true,
  "failed": false,
  "finished": true,
  "recognitionResult": {
    "lines": [{
      "boundingBox": [140, 289, 818, 294, 816, 342, 138, 340],
      "text": "General information Com",
      "words": [{
        "boundingBox": [106, 290, 363, 291, 363, 343, 106, 343],
        "text": "General"
      }, {
        "boundingBox": [323, 291, 659, 291, 659, 344, 323, 343],
        "text": "lawyer"
      }, {
        "boundingBox": [665, 291, 790, 291, 790, 344, 665, 344],
        "text": "Com"
      }]
    }]
  }
}

1 Ответ

0 голосов
/ 15 ноября 2018

Сначала используйте Quicktype.io для генерации собственного класса C #:

public partial class Result
    {
        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("succeeded")]
        public bool Succeeded { get; set; }

        [JsonProperty("failed")]
        public bool Failed { get; set; }

        [JsonProperty("finished")]
        public bool Finished { get; set; }

        [JsonProperty("recognitionResult")]
        public RecognitionResult RecognitionResult { get; set; }
    }

    public partial class RecognitionResult
    {
        [JsonProperty("lines")]
        public Line[] Lines { get; set; }
    }

    public partial class Line
    {
        [JsonProperty("boundingBox")]
        public long[] BoundingBox { get; set; }

        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("words")]
        public Word[] Words { get; set; }
    }

    public partial class Word
    {
        [JsonProperty("boundingBox")]
        public long[] BoundingBox { get; set; }

        [JsonProperty("text")]
        public string Text { get; set; }
    }

Затем десериализует JSON в экземпляр класса Result (назовем его result) с помощью Newtonsoft.

И тогда вы можете

result.RecognitionResult.Where(s => !string.IsNullOrEmpty(s.Text) && s.Text == "lawyer");

Если вы просто хотите первый случай, используйте .FirstOrDefault()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...