I have been using qt before, but I still want to use something familiar when writing C#, so I found this one
Qt wrote this before
// 定义 { } 对象
QJsonObject sessionObj;
// 插入元素,对应键值对
sessionObj.insert("type", "BIZ_CARD");
QJsonObject object;
object.insert("username", ui->lineEditUsername->text());
object.insert("password", ui->lineEditPassword->text());
object.insert("session", sessionObj);//防止重复登录
QJsonDocument doc;
doc.setObject(object);
QByteArray ba = doc.toJson(QJsonDocument::Compact);
qDebug()<<"data:::"<<ba;
In C#, there is also a similar way of writing
JSONObject session = new JSONObject();
session.Put("type", "BIZ_CARD");
JSONObject json = new JSONObject();
json.Put("username", "186xxxx4792");
json.Put("password", "123xx78");
json.Put("session", session);
Console.WriteLine(json.ToString());
This is how it is generated
{"username":"186xxxx4792","password":"123xxx678","session":{"type":"BIZ_CARD"}}
However, this requires a reference to a third-party dll, which is not included in Nuget. There is a tutorial in the compressed package after downloading.
You can refer to the source
[C#]JSONObject parses json, C# asp.net JSON parsing_c# jsonobject_cc_want's blog-CSDN blog
Github open source address:
https://github.com/CCwant/ForceJson