How can cast the string to json using C#?


How can cast the string to json using C#?



One string like this:


[["0.45842413","10"],["0.45850028","11"],["0.46092215","10"],["0.478999","133.69218728"]]



after cast like this:


[["Price":"0.45842413","Amount":"10"],["Price":"0.45850028","Amount":"11"],["Price":"0.46092215","Amount":"10"],["Price":"0.478999","Amount":"133.69218728"]]





the question is unclear
– yekanchi
Jul 1 at 3:38




2 Answers
2



Your question is unclear, both strings are JSON
You're basically asking how to cast from one format of JSON to another.
The best way to do it would be to



enter image description here





Please Don't post images of code. Surely it is more difficult than simply copy pasting code into the text editor.
– pinkfloydx33
Jul 1 at 9:05



You could use Regex.Replace with capture groups to insert "Price" and "Amount" into your string.


Regex.Replace



Something like:


using System;
using System.Text.RegularExpressions;

public class Program
{
public static void Main()
{
string data = "[["0.45842413","10"],["0.45850028","11"],["0.46092215","10"],["0.478999","133.69218728"]]";
data = Regex.Replace(data, "("\d+[.]{0,1}\d{0,}"),("\d+[.]{0,1}\d{0,}")", ""Price":$1,"Amount":$2");
Console.WriteLine(data);
}
}



Result:


[["Price":"0.45842413","Amount":"10"],["Price":"0.45850028","Amount":"11"],["Price":"0.46092215","Amount":"10"],["Price":"0.478999","Amount":"133.69218728"]]



Fiddle Demo






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter