-2

I'm working with a string array which contains json value in NETCORE C#. For ex:

{"Name":"Carl","Age":"18"},{"Name":"Rick","Age":"40"}

Is use Split(",") but the array gets only one record a the value gets cut at {"Name":"Carl". The result that I need is something like this:

string [0]=  "Name":"Carl","Age":"18"
string [1]=  "Name":"Rick","Age":"40"

Can someone explain me why comma doesn't work?

Details: don't suggest me to work it with json's packages because its something that needs to be done as shown. This Json string is stored in a db and needs to be read by my code, that's why I'm doing this.

I noticed that if i edit the json value like this:

{"Name":"Carl"|"Age":"18"};{"Name":"Rick"|"Age":"40"}

And split it by ; I get what I'm looking for.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 12
    If you have JSON, can't you just parse it? – Etienne de Martel Jul 26 '23 at 14:11
  • 4
    You say it is a _string array_. How are you calling `Split()`? Please create a [mre]. – 001 Jul 26 '23 at 14:15
  • String.Split() is basic, meaning there are all kinds of edge cases that can trip you up, as well as slower than a purpose-built parser, which will also ultimately do this in less code. – Joel Coehoorn Jul 26 '23 at 14:16
  • 4
    *"don't suggest me to work it with json's packages because its something that needs to be done as shown."* - Why can't you use a standard JSON parsing library to parse JSON? *"This Json string is stored in a db and needs to be read by my code, that's why I'm doing this."* - How does the use of a database (which is entirely irrelevant to parsing JSON) prevent the use of a JSON parsing library? If you split by comma then your result will be split by comma. In what way does that not match expectations? It's really not clear to me what problem you're trying to describe. – David Jul 26 '23 at 14:17
  • Does this answer your question? [How can I deserialize JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-deserialize-json-with-c) – Charlieface Jul 26 '23 at 20:22

2 Answers2

2

Can someone explain me why comma doesn't work?

The comma worked just as it was supposed to. This text has three commas, resulting in four records:

{"Name":"Carl","Age":"18"},{"Name":"Rick","Age":"40"}

becomes:

{"Name":"Carl"
"Age":"18"}
{"Name":"Rick"
"Age":"40"} 

And this is because string.Split() is not built for parsing JSON or other structued data. It really is simplistic, with no protection at all for commas within the data or any of a multitude of other edge cases. When one considers additional supporting code to account for those shortfalls it also tends to be slower than purpose-built parsers and ultimately tends to end up needing more code than a real parser, rather than less. All-in-all, it's best to avoid it for all but the most basic samples.

Instead, look at the System.Text.Json namespace. I know you asked not to use this kind of parser, but the given reason makes no sense: there's absolutely nothing stopping you from using this parser with strings retrieved from a database, and there are modes that will let you deserialize this without needing to define a class.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

Try to use string.Split("},{");

enter image description here

Example:

var str ="""{"Name":"Carl","Age":"18"},{"Name":"Carl","Age":"20"},{"Name":"Rick","Age":"40"}""";
str.Split("},{");
Rand Random
  • 7,300
  • 10
  • 40
  • 88
Albert
  • 11
  • 2
  • 1
    Of course, none of the resulting records are themselves valid JSON, so further parsing is only going to get more and more problematic. – David Jul 26 '23 at 14:25