2015-04-23 82 views
0

嘿所有我有在做此字符串JSON difficultys我有成一个对象,这样我可以这样使用它:JSON.net 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 =行( “@ application_uid”)的ToString。。错误是未将对象引用设置为对象的实例。

不太清楚我的全部,从而缺少能够检索行值,但显然它的东西....

+1

你为什么XML转换成JSON成一个对象?虽然不只是反序列化您的XML? –

+0

@ScottSimontis示例? – StealthRT

+0

我只是有点困惑与你正在尝试做什么。您能否从高层次解释您使用此代码要做什么? –

回答

0

......让这个JSON字符串,我有到对象,使我可以这样使用它:

json("go")("to")("needed")("spot").toString 

我想获得价值F_Paragraphtext,@application_uid & @uid。

Dim jstr As String = File.ReadAllText(filename) 

Dim myJ = JObject.Parse(jstr) 
Console.WriteLine("{0} == {1}", "F_Paragraphtext", 
     myJ("data")("records")("F_Form1")("F_Paragraphtext")) 
Console.WriteLine("{0} == {1}", "@application_uid", 
     myJ("data")("records")("F_Form1")("@application_uid")) 
Console.WriteLine("{0} == {1}", "@uid", 
     myJ("data")("records")("F_Form1")("@uid")) 

输出:

F_Paragraphtext == { “数据”:{ “DevisionName”: “测试服务加入后场”, “DevisionCode”: “测试”, “CodeInString”:”测试^测试| 2015年4月15日| 50%|测试^测试| 2015年4月15日| 25%|测试^测试| 2015年4月23日| 50%〜测试^测试| 2015年4月23日| N/A |测试^测试| 2015年4月8日| N/A |测试^测试| 2015年3月31日| N/A〜测试^测试| 2015年4月10日| N/A |测试^测试| 5 /二千〇一十五分之一| 50%|测试^测试| 2015年4月18日| N/A〜测试^测试| 2015年3月30日| 50%|测试^测试| 2015年4月24日| 50%|测试^测试| 2015年5月9日| 100%〜测试^测试| 2015年3月30日| 25%|测试^测试| 2015年3月30日| 100%|测试^测试| 2015年4月11日| 75%” }}
@application_uid == 2a667c5 9-225c-4954-8e04-77bb083e5180
@uid == 61f876f4-9760-4013-85bb-37965cff1fb6