0
{

"FirstName" :"Kaustubh",
"LastName" :" Sawant ",
"StartTime" :"01:00:00"

}

I want to get printed

First Name: Kaustubh
Last Name: Sawant
Start time : 01:00:00

This is the demo code written the actual object retrieves more values. Actual code :

String JsonValue=JsonConvert.SerializsObject(object);

also used Formatting.Indented and tried

Regex.Replace(JsonValue,@"\{},"","").Trim();

and then wrote into file using StreamWriter.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 2
    When you remove the `{` it will no longer be a JSON. But this is not the problem. To help you, we need to know your code. How do you write the output? When you use a third party library/function, it may be impossible to alter the output without changing the code of the library. And your sample output has a totally different style. It would be better to make a custom "Save" function. – Julo Feb 02 '19 at 04:42
  • i have converted the json object to string ,perform formatting.indented.I just want write the object without { " : , because i m going to send this text file to user.I have use newtonsoft.json – shivani chabuk Feb 02 '19 at 04:48
  • You might be able to do this with a custom `JsonWriter` similar to the ones shown in [Custom JSON Derivative Format](https://stackoverflow.com/q/30610196/3744182) or [JsonConverter Keyvalues with = instead of : separator](https://stackoverflow.com/q/48267668/3744182). – dbc Feb 02 '19 at 06:02

2 Answers2

1

I think you are attacking this from the wrong angle, if this is truly json, you are best to deserialize it using something like json.net, and then building the string up out of the properties.

However, because you insist on doing it via replacement, you can make use of regex

var result = Regex.Replace(input, @"[,{}\"")]", "").Trim();
Console.WriteLine(result);

Output

FirstName :Kaustubh
LastName : Sawant 
StartTime :01:00:00

Full Demo Here

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

Considering json is a string variable containing your input:

var json = @"
{
""FirstName"" :""Kaustubh"",
""LastName"" :"" Sawant "",
""StartTime"" :""01:00:00""
}";

Simple solution relying on standard library:

Console.WriteLine(json.Replace("{", "").Replace("}", "").Replace("\"","").Replace(",", "").Trim());

A solution using Newtonsoft.Json framework for deserializing json string:

var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

foreach (var entry in dict)
{
    Console.WriteLine($"{entry.Key}: {entry.Value}");
}
Cosmin Sontu
  • 1,034
  • 11
  • 16
  • String jsonValue= JsonConvert.SerializeObject(object, Formatting.Indented); I have use this the output is the json object which is written in file.I want to structurized the written content into file,So that I could sent this file to user via email. – shivani chabuk Feb 02 '19 at 12:51
  • If you have a user object and want to put it in an email, then maybe you don't need json, but proper text or html formatting depending on how you want or email. You could use StringBuilder to create a html table with one or more users. – Cosmin Sontu Feb 02 '19 at 14:09
  • I don't want to put a user object in email but sort and retrieve data and store on file and send it to user. The object retrieves the values of appointment details such as timing, problem,description,date etc he has taken. – shivani chabuk Feb 02 '19 at 14:14