【问题标题】:VB.NET - Convert Json to Xml using Json.NETVB.NET - 使用 Json.NET 将 Json 转换为 Xml
【发布时间】:2015-10-30 13:42:42
【问题描述】:

有没有什么方法可以将直接 url 到 json 转换成 xml 作为文本到一个文本框?

例子:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Json1 as string = "http://pastebin.com/raw.php?i=p3uBzBtm"
    Dim jss = New JsonSerializer()
    Dim response2 = jss.Deserialize(Of Object)(Json1)
    textbox1.text = response2
End Sub

抱歉这个不好的例子,我是这门语言的新手。

【问题讨论】:

  • 预期的输出(Textbox1 的文本)是什么?
  • 我想成为转换后的xml结果的地方。
  • 知道了。查看答案。

标签: json vb.net json.net


【解决方案1】:

使用此代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Json1 As String = New WebClient().DownloadString("http://pastebin.com/raw.php?i=p3uBzBtm")
    Dim str = JsonConvert.DeserializeXmlNode(Json1)
    TextBox1.Text = str.OuterXml
End Sub

对于多个节点,你会想要这样的东西:

Dim Json1 As String = "{ 'root': " & New WebClient().DownloadString("http://pastebin.com/raw.php?i=ugZrw4d6") & " }"
Dim doc As XmlDocument = JsonConvert.DeserializeXmlNode(Json1)
Dim result As String = doc.ChildNodes(0).InnerXml
TextBox1.Text = result

【讨论】:

  • 成功了,非常感谢!但是我看看我是否想用另一个具有多行和节点的下载字符串 url 来更改它给我的错误。你能优化这个链接吗? pastebin.com/raw.php?i=ugZrw4d6
  • 由于有效的 xml 只有一个根节点,因此您必须将 JSON 封装在一个对象中。查看更新的答案。
  • 这正是我想要的。非常感谢您的惊人帮助!
  • @qckmini6 考虑接受这个答案。它将帮助其他人知道这是一个有用的解决方案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多