How can I replace the following using Regex.Replace in my c# application?
Replace all of the following:
rendition\":{
with:
rendition\":[{
So i am adding the left square bracket.
and also
Replace:
\"}}
With:
\"}]}
I am using JsonConvert from NewtonSoft to convert XML to json. The rendition element in my xmls may or may not be an array. I am trying to force the converted json to an array
Replace:
(?<=rendition:)\"\{(.*?)\"\}\}
With:
\"[{$1\"}]}
In c#:
string json2 = Regex.Replace(json, "(?<=rendition\\\\\":)\\{(.*?)\\}\\}", "[{$1}]}");
See a live demo of this C# code running on ideone.
Related
I got some trouble with a problem when use Newtonsoft json.net to deserialize json string to dictionary. It 's a case of my json string have some special character.
string jsonString = "{\"name\":\"Jones Smith\",\"age\":\"20\",\"description\":\"The one live with \"ALIGATOR\"\"}";
Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
I try to find a solution in the use of json.net but i not found. So the FINAL plan is remove that "characters". So, what is the best solution for this case?
I think you can't do very much in your situation besides changing the format at the origin. The problem with your input is that there are " characters escaped the same way once in your json directly and once in your json values.
Consider the following part: "description":"The one live with "ALIGATOR""
How should a deserializer know which " should be considered part of the value or part of the json format?
I got the answer, like the last comment, that 's not valid JSON, below is valid JSON
{"name":"Jones Smith","age":"20","description":"The one live with \"ALIGATOR\""}
And all i can do is add '\' before special characters if the value of field description is "The one live with "ALIGATOR"" to make a valid JSON and convert to c# like this:
string jsonString = {\"name\":\"Jones Smith\",\"age\":\"20\",\"description\":\"The one live with \\"ALIGATOR\\"\"}
I'm somehow having troubles deserializing a json string into a simple List or string[] (I don't care which).
As of what I know, this is how to do this job:
JsonConvert.DeserializeObject<List<string>>(jsonString);
Here I am getting a RuntimeBinderException. It complains about the parameter, although my json string is valid and simple: a:1:{i:0;s:10:"Sahibinden";}
What you have isn't JSON is a serialized PHP object. There have been some tools that work well with this in C# but there isn't native support. If you own the PHP, then convert the object/array to JSON first. If not try the information on this question: https://stackoverflow.com/a/1923626/474702
Your JSON is invalid. Problems:
a:1 should be inside an object bracket of {}
The : before the { is invalid, you need a , there
The ; just after i:0 is invalid, you need a comma there
You repeat the mistake described in 1. and 2. inside your {} brackets as well
Solution: You need to read about JSON and make sure you understand its syntax.
So I'm working with umbraco and using the tag datatype. I'm trying to take all tags added to a given node and putting them into an array of strings but when I grab the value it always seems to come out like this:
"[\"Tag1\",\"Tag2\"]"
How can I convert this string of an array back into a regular array? All I have gotten so far was a string of individual characters
The array format you have provided as an example looks like part of the JSON object.
You could use JSON.net library to parse array token of the JSON object.
var array = JArray.Parse(tagString).Values<string>();
A complete example is available here.
You could try:
string[] newArray = item.Replace("\"", "").Replace("[", "").Replace("]", "").Split(',');
This would output as Tag1, Tag2 etc...
Hope this helps.
Use this
var tagString = "[\"Tag1\",\"Tag2\",\"Tag3\",\"Tag4\"]";
var tags = tagString.Trim().TrimStart('[').TrimEnd(']').Split(',');
(Using Newtonsoft.Json 6.0.6)
Given this code:
var folderNames = new List<string> { #"C:\Hello", #"C:\World" };
string json = JsonConvert.SerializeObject(folderNames);
Console.WriteLine(json);
Here is what the serialized json looks like (notice there are 4 backslashes):
"[\"C:\\\\Hello",\"C:\\\\World\"]"
I get this Console output (e.g. unescaped json):
["C:\\Hello","C:\\World"]
I don't want the extra escaping backslashes to appear in the Console output. What is the recommendation?
(Code also available on .NET Fiddle: https://dotnetfiddle.net/yHJvfu)
Javascript is escaping the slash the same way c# would. \ is an escape character in javascript. The JSON is valid
I have trouble using Regex to split a text file of JSON objects into string. The array of JSON objects are downloaded from an url and is meant to be processed by some javascript function. But I want to read them in C#. I have downloaded the file and just need to split it into individual JSON objects. The format of the text file is:
{......},{"S":...}
So I want to split it into a string[] so each JSON object is a string:
{"S":...}
{"S":...}
{"S":...}
{"S":...}
I want to leave out the comma that separates them in the original text file.
string[] jsons = Regext.Split(txtfile, "\{\"S\":");
But this doesn't work. How can I split it correctly?
If you aren't aware already this is a great tool http://regexr.com?36u96
Try
string[] splits = Regex.Split(txtfile, #"(?<=\}),");
You can use the JsonTextReader class provided by the Newtonsoft.JSON assembly (acquirable through NuGet).