简体   繁体   English

如何在c#中将字符串转换为json结构

[英]How to convert a string to a json structure in c#

I have the following string:我有以下字符串:

DBUser = XXX; 

I would like to convert the string to a json format so it would be something like:我想将字符串转换为 json 格式,所以它会是这样的:

{"DBUSER" : "XXX"}

The solution that I tried is:我尝试的解决方案是:

return "{ " + @"""DBUser""" + ":"+ "\"{x}\"" + "}" ;

The result that I get which not matching the json structure is我得到的结果与 json 结构不匹配

"{ \"DBUser\":\"{x}\"}"

Create an object.创建一个对象。

    var user = new User()
    {
      DBUser = "XXX"
    };

Then serialize the object.然后序列化对象。

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq; 
    var v = (JObject)JsonConvert.SerializeObject(user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM