0

I need to create json string which looks like this:

{
  "key0": [{ "key00": "value00" }, { "key00": "value01" }, ...], 
  "key1": [{ "key11": "value10" }, { "key11": "value11" }, ...],
  ......
}

For generation of this string I use string operations and regular expressions. But perhaps there is a way to make it easier? For example, using Newtonsoft.Json?

6
  • 3
    Create the object structure in C# and serialize it to JSON? Commented Feb 1, 2024 at 21:23
  • 1
    You use regular expressions to generate a string? First of all: how? Secondly: why? Thirdly: yes, use Newtonsoft.Json or System.Text.Json, or whatever Json lib out there. Commented Feb 1, 2024 at 21:27
  • 1
    "Yes! there is". Try it, I bet you will like it. Commented Feb 1, 2024 at 21:28
  • 2
    I'd recommend you start with System.Text.Json rather than Newtonsoft.Json. If your "keys" are dynamic (literally "key0", "key1", etc.) you can use a Dictionary with strings as the keys. If they're known at compile-time, I'd declare a class to represent the model and serialize to JSON. Commented Feb 1, 2024 at 21:33
  • Check Microsoft Docs: How to write .NET objects as JSON (serialize) Commented Feb 1, 2024 at 21:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.