How to create JSON from strings without creating a custom class using JSON.Net library
Solution 1
As mentioned in the comments, you could just as easily have created it using anonymous objects.
private string CreateJson(string val1, string val2, string val3, string val4, string val5, string val6) {
var configs = new[]
{
new { FirstKey = val1, SecondKey = val2, ThirdKey = val3},
new { FirstKey = val4, SecondKey = val5, ThirdKey = val6}
};
var jsonData = JsonConvert.SerializeObject(configs);
return jsonData;
}
Solution 2
Getting few clues from [@code4life][1]'s comment in accepted answer, I found out that it is achievable via JArray
object as well found under Newtonsoft.Json.Linq
namespace.
using Newtonsoft.Json.Linq;
private string CreateJson(string val1, string val2, string val3, string val4, string val5, string val6)
{
var configs = new[]
{
new { FirstKey = val1, SecondKey = val2, ThirdKey = val3},
new { FirstKey = val4, SecondKey = val5, ThirdKey = val6}
};
return JArray.FromObject(configs).ToString();
}
Note: Anonymous types which are being created through new { FirstKey = val1, SecondKey = val2, ThirdKey = val3}
syntax can contain any .NET data type and not just strings which I've asked in my original post e.g.new { FirstKey = "AnyString", SecondKey = true, ThirdKey = DateTime.Now}
[1]: https://stackoverflow.com/users/215741/code4life
Comments
-
RBT almost 2 years
I have this method in which I'm trying to create JSON string:
//my current file using Newtonsoft.Json; string key1 = "FirstKey"; string key2 = "SecondKey"; string key3 = "ThirdKey"; private string CreateJson(string val1, string val2, string val3,string val4, string val5, string val6) { //process the six arguments and three key-related member variables to create a JSON array //don't want to use JsonConvert.SerializeObject the way I'm doing below as it requires creating a class var configs = new List<CustomClass> { new CustomClass{ FirstKey = val1,SecondKey= val2,ThirdKey= val3} , new CustomClass{ FirstKey= val4,SecondKey= val5,ThirdKey = val6} }; var jsonData = JsonConvert.SerializeObject(configs); return jsonData; } //A class in another file public class CustomClass { public string FirstKey { get; set; } public string SecondKey{ get; set; } public string ThirdKey{ get; set; } }
I'm trying to create the JSON array using JSON.Net. The expected output is as below:
[{"FirstKey":val1,"SecondKey":val2,"ThirdKey":val3} , {"FirstKey":val4,"SecondKey":val5,"ThirdKey":val6}]
Here
val1
toval6
values should get replaced by the argument values at run-time.Initially, since there were just three types of string key-value pairs, so I thought it would be pretty straightforward to create a JSON string simply by using string literals and appending then one after the other in JSON format. But soon I stumbled upon the world of escape characters which can deform a JSON string e.g.
\r
.I had been using JSON.Net library in the past simply to serialize and deserialize objects using
JSONConvert
class and I never cared and was completely unaware about this handling of escape characters by JSON.Net library does behind the scene for us to keep the JSON string valid.Anyways, coming back to my problem. I was able to solve my problem by creating a custom class having three properties
FirstKey
,SecondKey
, andThirdKey
. Then, create an object of the class, then assign the values in argumentsval1
andval2
to then and then useJsonConvert.SerializeObject
API.I want a very simply way of creating JSON string using JSON.Net NuGet package without involving custom classes. Creating a class
CustomClass
altogether feels like an overhead here. I'm looking for something of sort of likeStringBuilder.Append
API if it is available in the JSON library I'm using. I'm not sure if I'm missing any of the APIs in JSON.Net. -
code4life almost 7 yearsYou can even use
JObject.FromObject
too! -
RBT almost 7 years@code4life although Nkosi's solution is pretty straightforward and what I was looking for but still I tried to accomplish it using
JObject.FromObject
also but I'm gettingSystem.ArgumentException
exception sayingObject serialized to Array. JObject instance expected.
. Can you please help me with some bare minimum code snippet to achieve the same usingJObject.FromObject
if it is possible? -
RBT almost 7 yearsI got it @code4life. Actually it was
JArray.FromObject
API. -
Sayed Muhammad Idrees almost 3 yearsI see this an Absolute win.