C# json生成

之前一直用qt的,写C#还想用熟悉的,就找到这样一个

之前qt这样写的

// 定义 { } 对象
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;

在C#里面,也有类似的写法

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());

 生成的就这样

{"username":"186xxxx4792","password":"123xxx678","session":{"type":"BIZ_CARD"}}

不过这个要引用第三方的dll,Nuget里面没有,下载后压缩包里面有教程

可以参考来源

[C#]JSONObject解析json,C# asp.net JSON解析_c# jsonobject_cc_want的博客-CSDN博客

Github开源地址:

https://github.com/CCwant/ForceJson

猜你喜欢

转载自blog.csdn.net/title71/article/details/131291528