【发布时间】:2015-04-23 20:09:02
【问题描述】:
嘿,我很难将我拥有的这个字符串 JSON 变成一个对象,以便我可以像这样使用它:
json("go")("to")("needed")("spot").toString
并获得我的价值。
目前 XML 到 JSON 看起来像这样:
{
"?xml": {
"@version": "1.0",
"@encoding": "UTF-8"
},
"data": {
"recordCount": "1",
"metadata": {
"elementType": [
{
"name": "F_Paragraphtext",
"uiType": "textArea",
"dataType": "string",
"label": "Text String",
"dataLabel": ""
}
]
},
"records": {
"F_Form1": {
"@application_uid": "2a667c59-225c-4954-8e04-77bb083e5180",
"@uid": "61f876f4-9760-4013-85bb-37965cff1fb6",
"F_SingleLine": "test",
"F_Paragraphtext": "{\"Data\":{\"DevisionName\":\"testing service after added field\",\"DevisionCode\":\"test\",\"CodeInString\":\"test^test|4/15/2015|50%|test^test|4/15/2015|25%|test^test|4/23/2015|50%~test^test|4/23/2015|N/A|test^test|4/8/2015|N/A|test^test|3/31/2015|N/A~test^test|4/10/2015|N/A|test^test|5/1/2015|50%|test^test|4/18/2015|N/A~test^test|3/30/2015|50%|test^test|4/24/2015|50%|test^test|5/9/2015|100%~test^test|3/30/2015|25%|test^test|3/30/2015|100%|test^test|4/11/2015|75%\"}}"
}
}
}
}
我正在尝试获取 F_Paragraphtext、@application_uid 和 @uid 的值。
我拥有的 VB.net 代码:
Dim doc1 As XmlDocument = New XmlDocument()
doc.LoadXml(serverResponse)
Dim json1 As String = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented)
jsreader = New Newtonsoft.Json.JsonTextReader(New StringReader(json1))
Try
json = DirectCast(New Newtonsoft.Json.JsonSerializer().Deserialize(jsreader), Newtonsoft.Json.Linq.JObject)
Catch ex As Exception
MsgBox(ex.Message)
End Try
For Each Row In json("data")("records")(febFormID)
Try
dName = json("@application_uid").ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
而行值为:
{"@application_uid": "2a667c59-225c-4954-8e04-77bb083e5180"}
错误发生在 dName = Row("@application_uid").ToString。错误是对象引用未设置为对象的实例。。
不太确定为了能够检索 Row 值我缺少什么,但显然它是什么......
【问题讨论】:
-
为什么要将 XML 转换为 JSON 转换为对象?而不仅仅是反序列化您的 XML?
-
@ScottSimontis 示例?
-
我只是对你想要做的事情有点困惑。您能否从高层次解释一下您要如何处理这段代码?