A
A
andrey_levushkin2018-09-24 16:14:13
.NET
andrey_levushkin, 2018-09-24 16:14:13

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}

(there may be more segments like {"userId":"XXX"})
How to make it so that we get the following as an output?:
111
222
333
444
(either in a text file or in a Text.Box)
I had the idea is to parse the text, because the places I need are always between {"userId":" and "}
That is, you can make a loop that will run through the text of this variable and select everything between {"userId":" and "}, putting expressions to newlines
But how to do it? Any ideas? thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2018-09-24
@mindtester

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-bla
chop off
ps keep the prototype, contains all 3 mentioned approaches, but the performance can only be compared on really big data)).
вопрос понравился, и выдалось время.. хотя и не за 15 минут, врать не буду, часа полтора порядка 45 минут ушло на заковырки )) в основном с json (мало опыта)
upd и уже исправлено - была заготовка класса для частичной json выборки, но она не нужна
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}");
            }
        }
    }
}

R
Roman Mirilaczvili, 2018-09-24
@2ord

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 question

Ask a Question

731 491 924 answers to any question