如何从字符串创建JSON而不使用JSON.Net库创建自定义类 [英] How to create JSON from strings without creating a custom class using JSON.Net library
问题描述
我有一个非常简单的方法,如下所示:
I have this very simple method as below:
//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; }
}
我正在尝试使用JSON.Net创建JSON数组.预期输出如下:
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}]
此处val1
至val6
的值应在运行时替换为参数值.
Here val1
to val6
values should get replaced by the argument values at run-time.
最初,由于只有三种类型的字符串键-值对,因此我想简单地通过使用字符串文字并以JSON格式一个接一个地附加一个JSON字符串来创建JSON字符串会非常简单.但是很快我偶然发现了转义字符的世界,这些字符可以使JSON字符串变形,例如\r
.
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
.
我过去一直在使用JSON.Net库来简单地通过JSONConvert
类对对象进行序列化和反序列化,但我从不在意,并且完全不了解JSON.Net库对这种转义字符的处理,这在后台为我们做了保持JSON字符串有效.
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.
无论如何,回到我的问题上来.通过创建具有三个属性FirstKey
,SecondKey
和ThirdKey
的自定义类,我能够解决我的问题.然后,创建该类的对象,然后将val1
和val2
自变量中的值分配给then,然后使用JsonConvert.SerializeObject
API.
Anyways, coming back to my problem. I was able to solve my problem by creating a custom class having three properties FirstKey
, SecondKey
, and ThirdKey
. Then, create an object of the class, then assign the values in arguments val1
and val2
to then and then use JsonConvert.SerializeObject
API.
我想要一种使用JSON.Net NuGet包创建JSON字符串的非常简单的方法,而无需涉及自定义类.完全创建类CustomClass
感觉很麻烦.我正在寻找某种StringBuilder.Append
API,如果它在我正在使用的JSON库中可用.我不确定是否缺少JSON.Net中的任何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 like StringBuilder.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.
推荐答案
如注释中所述,您可以使用匿名对象轻松地创建它.
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;
}
这篇关于如何从字符串创建JSON而不使用JSON.Net库创建自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!