E
E
ErrorInever2016-10-23 16:50:21
JSON
ErrorInever, 2016-10-23 16:50:21

C#. json. How to get array elements from an array?

json file

{
  "ts":123456,
  "pts":123,
  "updates":
}

I get a line like:
string json = "{\"ts\":1645337136,\"pts\":105,\"updates\":}\r\n";

It is necessary to extract the first "First" and the last "Text" element from the array, which is located in the "updates"
array. I used the Json.net library, I managed to extract only the ts and pts values.
using System;
using Newtonsoft.Json;

class Events                                
    {
        public string ts { get; set; }
        public string pts { get; set; }
    }
static void Main(string[] args)
   {
string json = ""{\"ts\":123456,\"pts\":123,\"updates\":}\r\n";
Events events = JsonConvert.DeserializeObject<Events>(json);
   }

I can't figure out how to describe the json structure itself in the class and how to work with it.
Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoliy Mikhailov, 2016-10-24
@ErrorInever

class Events                                
    {
        public string ts { get; set; }
        public string pts { get; set; }
        public object[][] updates {get;set;}
    }
...
Events events = JsonConvert.DeserializeObject<Events>(json);
var first = events.updates.FirstOrDefault(f=>f!=null&&f[0]=="First");//получение первого массива содержащего слово "First"
var text = events.updates.FirstOrDefault(f=>!(f!=null&&f.Any(a=>a=="Text")))//получение массива в котором есть "Text"
if(text==null)
    throw new ArgumentException(...);
var textIndex = text.TakeWhile(w=>!(w=="Text")).Count();//получение индекса в массиве для слова "Text"
 ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question