Answer the question
In order to leave comments, you need to log in
C# - How to make a selection from the text?
The variable contains text, for example:
string a = "{"anchor":"aaabbbccc==","members":[{"userId":"111"},{"userId":"2222"},{"userId":"333"},{"userId":"444"}],"has_more":true}
Answer the question
In order to leave comments, you need to log in
https://www.newtonsoft.com/json
if you are offered regexes, you should compare performance (regulars don’t shine with upd speed , that’s right, simplicity too, we don’t consider trivial cases)))
I would most likely do it hand-to-hand, provided that others data is not needed. what is the essence of what you have already described ;))) and in terms of performance, this is probably the fastest option ( upd ) in terms of performance, but in terms of implementation - it depends on skills, it may be easier for someone newtonsoft (well, see the lazy option, there is also no performance (but I think it’s far from the slowest), but it’s easy to quickly outline the logic and easily test it)
ps You can also use the lazy option - Split by
{"userId":"then we discard the first element, all the rest begin with the desired value, we only need the tail
"}bla-bla-blachop off
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace parsejson111
{
class Program
{
const string data = "{\"anchor\":\"aaabbbccc==\",\"members\":[{\"userId\":\"111\"},{\"userId\":\"2222\"},{\"userId\":\"333\"},{\"userId\":\"444\"}],\"has_more\":true}";
// фишки замера заложены, но разница может проявиться только на больших объемах ))
static void Main(string[] args)
{
Console.WriteLine("data:");
Console.WriteLine(data);
var sw = new Stopwatch();
sw.Start();
var ts = sw.Elapsed; // лень объявлять тип
json();
hand();
lazy();
sw.Stop();
Console.WriteLine("press any key to continue...");
Console.ReadKey();
void json() // возможно существует более элегантный вариант. тут у меня опыта мало ))
{
sw.Reset();
Console.WriteLine("... json");
var j = JObject.Parse(data);
var r = j["members"].Children().ToList();
foreach (var u in r)
{
var uid = JObject.Parse(u.ToString());
var id = uid["userId"].Value<string>();
Console.WriteLine(id);
}
ts = sw.Elapsed;
Console.WriteLine($"\t{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds:000}");
}
void hand() // зато тут гарантирую - оптимизировать лучше не реально, и по перфомансу вряд ли можно лучше ))
{
sw.Reset();
Console.WriteLine("... hand");
var p = 0;
while ((p = data.IndexOf("{\"userId\":\"", p)) > 0)
{
p += 11; //длина шаблона. заодно избегаем риска зацикливания
var f = data.IndexOf("\"}", p);
Console.WriteLine(data.Substring(p, f - p));
}
ts = sw.Elapsed;
Console.WriteLine($"\t{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds:000}");
}
void lazy()
{
sw.Reset();
Console.WriteLine("... lazy");
var t = data.Split(new string[] { "{\"userId\":\"" }, StringSplitOptions.RemoveEmptyEntries);
for (var i = 1; i < t.Length; i++) // здесь и везде - обработка пустого множества на вашей совести
{
var id = t[i].Split(new string[] { "\"}" }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(id[0]);
}
ts = sw.Elapsed;
Console.WriteLine($"\t{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds:000}");
}
}
}
}
It’s not worth disassembling with regulars, you can step on a rake.
Better to parse with System.Json or Json.NET (Newtonsoft).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question