我想在没有双引号的情况下为现有的Json增加价值

时间:2017-03-01 12:04:52

标签: c# .net json json.net

最初,我喜欢这样的Json

{
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

我想在“softwareName”之前添加" status":{success:true}我已经为此做了

JObject rss = JObject.Parse(jsonString);
rss.Property("softwareName").AddBeforeSelf(new JProperty("status", "{success: true}"));

给我这样的结果

{
  "status": "{success: true}",
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

但是,我希望得到这样的结果;

{
  "status": {success: true},
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

即没有双引号的状态值。我怎样才能达到这个理想的效果?因为在那之后我会有如下所示的观点

enter image description here

但目前我喜欢这个

enter image description here

2 个答案:

答案 0 :(得分:3)

你想这样做:

JObject rss = JObject.Parse(jsonString);
rss.Property("softwareName").AddBeforeSelf(new JProperty("status", JObject.Parse("{success: true}")));

基本上,在将字符串添加为属性之前,需要将字符串解析为JSON对象。

答案 1 :(得分:2)

试试这个

obj  = {
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
}

var newKey = "status"
var newVal = {success : true}

obj[newKey] = newVal

它给出了

obj  = {
  "softwareName": "MYSOFT",
  "softwareVersion": "0.4.5.9",
  "TimeZone": "2017-03-01T11:30:18.764103"
  "status": {success : true}
}