Json回應如下:
{
"Status": "1",
"Result": {
"Response": "Ok",
"TotalRecord": 2
},
"List": [
{
"ID": "1",
"Name": "Karen",
"PhoneNo": "0010000114248"
},
{
"ID": "2",
"Name": "Krystal",
"PhoneNo": "0010000114250"
}
]
}
代碼如下:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Net
Imports Newtonsoft.Json
Imports System.IO
Imports System.Web.Script.Serialization
Imports Newtonsoft.Json.Linq
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class test
Inherits System.Web.Services.WebService
<WebMethod> _
Public Function Post()
Dim apiUrl = ConfigurationManager.AppSettings("http://192.XX.XXX.XX/api/Update")
Dim request As WebRequest = WebRequest.Create(apiUrl)
Dim body = "{""PageIndex"": ""1"", ""PageSize"": ""10000""}"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(body)
request.Method = "POST"
request.ContentType = "application/json"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)
Dim rte As String = sr.ReadToEnd()
Dim jsonObject As JObject = JObject.Parse(rte)
Dim id = If(jsonObject.SelectToken("List")(0)("ID") Is Nothing, "", jsonObject.SelectToken("List")(0)("ID").ToString)
Dim name = If(jsonObject.SelectToken("List")(0)("Name") Is Nothing, "", jsonObject.SelectToken("List")(0)("Name").ToString)
Dim phoneNo = If(jsonObject.SelectToken("List")(0)("PhoneNo") Is Nothing, "", jsonObject.SelectToken("List")(0)("PhoneNo").ToString)
Dim dt As DataTable = New DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
dt.Rows.Add(id, name, phoneNo)
Dim ds As DataSet = New DataSet("Result")
ds.Tables.Add(dt)
Return ds.GetXml()
End Function
當我嘗試這段代碼時,它成功轉換為數據集,但是它只返回一條記錄, 我也無法獲取Json回應中的第二條記錄,而且如果我有超過 10000 條記錄,我如何才能獲得數據集中的許多記錄.求大大解答謝謝
我看你有安裝這個Json~那你應該可以跟我用一樣的方式~
Imports Newtonsoft.Json
下面程式碼是抓Json這段出來解析跑陣列
[
{
"ID":"1",
"Name":"Karen",
"PhoneNo":"0010000114248"
},
{
"ID":"2",
"Name":"Krystal",
"PhoneNo":"0010000114250"
}
]
'先抓第一層的List陣列字串
Dim Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(rte)
Dim ListArray As String = Newtonsoft.Json.JsonConvert.SerializeObject(Obj.Item("List"))
'建立新的空白DataTable
Dim dt As System.Data.DataTable = New System.Data.DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
'將陣列字串丟到迴圈紀錄到DataTable
Dim jsonObject As Newtonsoft.Json.Linq.JArray = Newtonsoft.Json.Linq.JArray.Parse(ListArray)
For j = 0 To jsonObject.Count - 1
Dim id As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("ID")), """", "")
Dim name As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("name")), """", "")
Dim phoneNo As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("phoneNo")), """", "")
dt.Rows.Add(id, name, phoneNo)
Next
'再把DataTable紀錄到DataSet顯示
Dim ds As System.Data.DataSet = New System.Data.DataSet("Result")
ds.Tables.Add(dt)
純真的人請問我應該返回什麼才能獲得資料,因為我使用斷點,它顯示數據已正確循環資料或者如何將此這些資料寫入sql server數據庫表中?
因為是Json擷取指定陣列資料~
我是用組合SQL字串(insert into)組合好後~寫入資料庫~@@
感謝大大,懂了
1,把你的JSON轉成CLASS
https://jsonutils.com/
Public Class Result
Public Property Response As String
Public Property TotalRecord As Integer
End Class
Public Class List
Public Property ID As String
Public Property Name As String
Public Property PhoneNo As String
End Class
Public Class Example
Public Property Status As String
Public Property Result As Result
Public Property List As List()
End Class
2、到nuget 安裝newtonsoft.json
3、把你的json字串,用
Dim newObject as Example = newtonsoft.jsonConvert.deserialize<Example>(strYourJSON)
' 然後你要的就是
print( newObject.Result.Response ) ' OK
你再寫程式去把這個 newObject 裡的東西,塞回資料庫就得了