我目前正与外方合作。他们最近为我正在解析的json字符串添加了一些标准。我正在将此字符串加载到数据表中,因此必须删除字符串的一部分才能加载而没有错误。目前我只是删除字符串本身的提款和存款。然后我使用JsonConvert.DeserializeObject将String加载到数据表中。
以下是我目前所做的一个例子。我也试图删除下面的"地址" JSON。将.Replace(地址,"")添加到我的jsonstring变量时,它不会像存款或取款那样删除。
这是执行此操作的正确方法还是有更好的方法来删除JSON部分?
他们添加的json是:
"address": {
"address1": "9261 Test St.",
"address2": "",
"city": "Irvine",
"state": "CA",
"zip": "92618"
},
rmvResults = "{" & ControlChars.Quote & "results" & ControlChars.Quote & ":"
Dim deposits As String = "," & ControlChars.Quote & "deposits" & ControlChars.Quote & ":[]"
Dim withdrawals As String = "," & ControlChars.Quote & "withdrawals" & ControlChars.Quote & ":[]"
Dim address As String = "," & ControlChars.Quote & "address" & ControlChars.Quote & ":{}"
jsonstring = syncClient.DownloadString(url).Replace(ControlChars.Quote, """").Replace(rmvResults, "").Replace(deposits, "").Replace(withdrawals, "").Replace(address, "") 'Removes beginning class.
jsonContent = jsonstring.Remove(jsonstring.Length - 1) 'removes last character (closing tag)
当前的JSON结构:
{
"results": [
{
"id": "",
"tp_id": "",
"firstname": "",
"lastname": "",
"company": "",
"address": {
"address1": "",
"address2": "",
"city": "",
"state": "",
"zip": ""
},
"phones": {
"home_phone": ,
"work_phone": ,
"cell_phone": ""
},
"custom_fields": {
"Contact Type": "",
"Vehicle Make": "",
"Vehicle Model": "",
"Vehicle Year": ""
},
"balance": ,
"pending_balance": ,
"fleet": "",
"deposits": [
{
"id": "",
"process_date": "2017-10-24",
"cleared_date": "2017-10-27",
"amount": "90.00",
"transID": "",
"memo": "",
"status": ""
}
],
"withdrawals": []
}
]
}
答案 0 :(得分:0)
我能够通过使用以下代码使其工作。
Public Shared Function Tabulate(json As String) As DataTable
Dim jsonLinq = JObject.Parse(json)
' Find the first array using Linq
Dim srcArray = jsonLinq.Descendants().Where(Function(d) TypeOf d Is JArray).First()
Dim trgArray = New JArray()
For Each row As JObject In srcArray.Children(Of JObject)()
Dim cleanRow = New JObject()
For Each column As JProperty In row.Properties()
' Only include JValue types
If TypeOf column.Value Is JValue Then
cleanRow.Add(column.Name, column.Value)
End If
Next
trgArray.Add(cleanRow)
Next
Return JsonConvert.DeserializeObject(Of DataTable)(trgArray.ToString())
End Function