如何使用json.net在Visual Basic中创建JSON字符串?

时间:2018-07-12 11:51:30

标签: json.net

我正在使用json.net解析JSON文本。我现在有一个JObject

Dim json_text As String
json_text = "{" & Chr(34) & "Venue" & Chr(34) & ": {" & Chr(34) & "ID" & Chr(34) & ":" & Chr(34) & "JSON" & Chr(34) & "}}"
Console.WriteLine(json_text)
Dim json As JObject
json = JObject.Parse(json_text)
Console.WriteLine(json.SelectToken("Venue").SelectToken("ID"))

现在我要做相反的事情,并从JSON变量创建JSON文本。

很遗憾,我在那里找不到任何解决方案。

1 个答案:

答案 0 :(得分:0)

    Imports Newtonsoft.Json.Linq
    Imports Newtonsoft.Json

    Dim json_text As String
    json_text = "{" & Chr(34) & "Venue" & Chr(34) & ": {" & Chr(34) & "ID" & Chr(34) & ":" & Chr(34) & "JSON" & Chr(34) & "}}"
    Console.WriteLine(json_text)
    Dim json As JObject
    json = JObject.Parse(json_text)
    Console.WriteLine(json.SelectToken("Venue").SelectToken("ID"))

    Dim json_string As String = JsonConvert.SerializeObject(json,
                   Formatting.Indented, New JsonSerializerSettings())

    Console.WriteLine(json_string)

结果是:

{"Venue": {"ID":"JSON"}}
JSON
{
  "Venue": {
    "ID": "JSON"
  }
}