Json回應如下:
{
"Status": "1",
"Result": {
"Response": "Ok",
"TotalRecord": 2
},
"List": [
{
"ID": "1",
"Name": "Karen",
"PhoneNo": "0010000114248",
"Data":{
"Access": "123456789",
"StartTime": "2023-08-15 00:00:00"
}
},
{
"ID": "2",
"Name": "Krystal",
"PhoneNo": "0010000114250",
"Data":{
"Access": "23466876",
"StartTime": "2023-08-17 00:00:00"
}
}
]
}
Dim Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(rte)
Dim ListArray As String = Newtonsoft.Json.JsonConvert.SerializeObject(Obj.Item("List"))
Dim dt As System.Data.DataTable = New System.Data.DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
dt.Columns.Add("access")
dt.Columns.Add("startTime")
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")), """", "")
Dim access As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("Data.Access")), """", "")
Dim startTime As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("Data.StartTime")), """", "")
dt.Rows.Add(id, name, phoneNo, access, startTime)
Next
之前有大大幫我解決如何將 json 轉換為字符串,但是之後在Json回應加了東西,用了自己的方法去處理,但是資料拿回來是null,不知道在哪出錯了,有大大知道什麼原因嗎?
1.錯誤訊息是?
2.ListArray宣告了什麼?
我還是比較習慣用class的方式,來做json的處理。
Newtonsoft.Json.JsonConvert: Serializing and Deserializing JSON in C# and VB.Net
class+1 這完全不知道在寫什麼 -0-
c#
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string json="{\"Status\":\"1\",\"Result\":{\"Response\":\"Ok\",\"TotalRecord\":2},\"List\":[{\"ID\":\"1\",\"Name\":\"Karen\",\"PhoneNo\":\"0010000114248\",\"Data\":{\"Access\":\"123456789\",\"StartTime\":\"2023-08-1500:00:00\"}},{\"ID\":\"2\",\"Name\":\"Krystal\",\"PhoneNo\":\"0010000114250\",\"Data\":{\"Access\":\"23466876\",\"StartTime\":\"2023-08-1700:00:00\"}}]}";
Sample myData = JsonConvert.DeserializeObject<Sample>(json);
Console.WriteLine("Status="+myData.Status.ToString()); Console.WriteLine("Result="+myData.Result.ToString());
for (int i=0;i<=myData.List.Count-1;i++)
{
Console.WriteLine("ID="+myData.List[i].ID); Console.WriteLine("Name="+myData.List[i].Name); Console.WriteLine("PhoneNo="+myData.List[i].PhoneNo);
}
}
}
public class Sample
{
public string Status { get; set; }
public result Result { get; set; }
public List<SampleList> List { get; set; }
}
public class result
{
public string Response { get; set; }
public int TotalRecord { get; set; }
}
public class SampleList
{
public string ID { get; set; }
public string Name { get; set; }
public string PhoneNo { get; set; }
public SampleListData Data { get; set; }
}
public class SampleListData
{
public string Access { get; set; }
public string StartTime { get; set; }
}
VB
Imports System.Collections.Generic
Imports Newtonsoft.Json
Public Class Program
Public Shared Sub Main()
Dim json = "{""Status"":""1"",""Result"":{""Response"":""Ok"",""TotalRecord"":2},""List"":[{""ID"":""1"",""Name"":""Karen"",""PhoneNo"":""0010000114248"",""Data"":{""Access"":""123456789"",""StartTime"":""2023-08-1500:00:00""}},{""ID"":""2"",""Name"":""Krystal"",""PhoneNo"":""0010000114250"",""Data"":{""Access"":""23466876"",""StartTime"":""2023-08-1700:00:00""}}]}"
Dim myData As Sample = JsonConvert.DeserializeObject(Of Sample)(json)
Console.WriteLine("Status=" & myData.Status.ToString())
Console.WriteLine("Result=" & myData.Result.ToString())
For i = 0 To myData.List.Count - 1
Console.WriteLine("ID=" & myData.List(i).ID)
Console.WriteLine("Name=" & myData.List(i).Name)
Console.WriteLine("PhoneNo=" & myData.List(i).PhoneNo)
Next
End Sub
End Class
Public Class Sample
Public Property Status As String
Public Property Result As result
Public Property List As List(Of SampleList)
End Class
Public Class result
Public Property Response As String
Public Property TotalRecord As Integer
End Class
Public Class SampleList
Public Property ID As String
Public Property Name As String
Public Property PhoneNo As String
Public Property Data As SampleListData
End Class
Public Class SampleListData
Public Property Access As String
Public Property StartTime As String
End Class
ok~我測試Json格式轉Class格式匯出~沒問題^_^
測試頁.(直接在網站上面測試~不是在VS軟體測試^^")
<%@ Page Title="" Language="VB" %>
<script runat="server">
Public Class Sample
Public Property Status As String
Public Property Result As result
Public Property List As List(Of SampleList)
End Class
Public Class result
Public Property Response As String
Public Property TotalRecord As Integer
End Class
Public Class SampleList
Public Property ID As String
Public Property Name As String
Public Property PhoneNo As String
Public Property Data As SampleListData
End Class
Public Class SampleListData
Public Property Access As String
Public Property StartTime As String
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rte As String = ""
rte &= " {"
rte &= " ""Status"": ""1"","
rte &= " ""Result"": {"
rte &= " ""Response"": ""Ok"","
rte &= " ""TotalRecord"": 2"
rte &= " },"
rte &= " ""List"": ["
rte &= " {"
rte &= " ""ID"": ""1"","
rte &= " ""Name"": ""Karen"","
rte &= " ""PhoneNo"": ""0010000114248"","
rte &= " ""Data"":{"
rte &= " ""Access"": ""123456789"","
rte &= " ""StartTime"": ""2023-08-15 00:00:00"""
rte &= " }"
rte &= " },"
rte &= " {"
rte &= " ""ID"": ""2"","
rte &= " ""Name"": ""Krystal"","
rte &= " ""PhoneNo"": ""0010000114250"","
rte &= " ""Data"":{"
rte &= " ""Access"": ""23466876"","
rte &= " ""StartTime"": ""2023-08-17 00:00:00"""
rte &= " }"
rte &= " }"
rte &= " ]"
rte &= " }"
Dim Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(rte)
Dim ListArray As String = Newtonsoft.Json.JsonConvert.SerializeObject(Obj.Item("List"))
Dim dt As System.Data.DataTable = New System.Data.DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
dt.Columns.Add("access")
dt.Columns.Add("startTime")
Dim DataStr As Newtonsoft.Json.Linq.JObject
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")), """", "")
Dim access As String = ""
Dim startTime As String = ""
Try
DataStr = Newtonsoft.Json.Linq.JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("Data")))
access = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("Access")), """", "")
startTime = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("StartTime")), """", "")
Catch ex As Exception
Response.Write("<br />" & ex.ToString)
End Try
dt.Rows.Add(id, name, phoneNo, access, startTime)
Next
Dim myData As Sample = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Sample)(rte)
Response.Write("<br />Status=" & myData.Status.ToString())
Response.Write("<br />Result=" & myData.Result.ToString())
For i = 0 To myData.List.Count - 1
Response.Write("<br />ID=" & myData.List(i).ID)
Response.Write("<br />Name=" & myData.List(i).Name)
Response.Write("<br />PhoneNo=" & myData.List(i).PhoneNo)
Next
End Sub
</script>
<br />test..
沒有 Data.Access Data.StartTime 這樣寫法...
每層Json位置~都是指定的~
所以下一層~只能先抓集合~在抓解析下一層~
Dim DataStr As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonObject(j).Item("Data"))
--得到此區段Json字串~
{
"Access": "123456789",
"StartTime": "2023-08-15 00:00:00"
}
Dim access As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("Access")), """", "")
Dim startTime As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("StartTime")), """", "")
Dim Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(rte)
Dim ListArray As String = Newtonsoft.Json.JsonConvert.SerializeObject(Obj.Item("List"))
Dim dt As System.Data.DataTable = New System.Data.DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
dt.Columns.Add("access")
dt.Columns.Add("startTime")
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")), """", "")
Dim DataStr As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonObject(j).Item("Data"))
Dim access As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("Access")), """", "")
Dim startTime As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("StartTime")), """", "")
dt.Rows.Add(id, name, phoneNo, access, startTime)
Next
那我寫這樣可以嗎?
可以唷~有錯的話~你再貼訊息~
畢竟直接推測寫法的~沒測試過@@..
我使用控制台應用程序編寫,在測試過程中突然關閉,並且沒有顯示錯誤
拿去實地環境試試吧~
我使用斷點,它在這段代碼之後停止
Dim DataStr As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonObject(j).Item("Data"))
有可能有Json區段沒有Data,你用try跳過試試~
改成以下方式試試~
Dim Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(rte)
Dim ListArray As String = Newtonsoft.Json.JsonConvert.SerializeObject(Obj.Item("List"))
Dim dt As System.Data.DataTable = New System.Data.DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
dt.Columns.Add("access")
dt.Columns.Add("startTime")
Dim DataStr As Newtonsoft.Json.Linq.JObject
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")), """", "")
Dim access As String = ""
Dim startTime As String = ""
Try
DataStr = Newtonsoft.Json.Linq.JObject.Parse(jsonObject(j).Item("Data"))
access = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("Access")), """", "")
startTime = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("StartTime")), """", "")
Catch ex As Exception
End Try
dt.Rows.Add(id, name, phoneNo, access, startTime)
Next
allenlwh你要照他的json寫一段程式碼給他看吧~
不然應該是看不懂的~
純真的人有這個錯誤
Accessed JObject values with invalid key value:0. Object property name expected.
我知道怎麼解決
Dim Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(rte)
Dim ListArray As String = Newtonsoft.Json.JsonConvert.SerializeObject(Obj.Item("List"))
Dim dt As System.Data.DataTable = New System.Data.DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
dt.Columns.Add("access")
dt.Columns.Add("startTime")
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")), """", "")
Dim access As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).SelectToken("Data.Access")), """", "")
Dim startTime As String = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).SelectToken("Data.StartTime")), """", "")
dt.Rows.Add(id, name, phoneNo, access, startTime)
Next
純真的人 我盡力了...
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string json="{\"Status\":\"1\",\"Result\":{\"Response\":\"Ok\",\"TotalRecord\":2},\"List\":[{\"ID\":\"1\",\"Name\":\"Karen\",\"PhoneNo\":\"0010000114248\",\"Data\":{\"Access\":\"123456789\",\"StartTime\":\"2023-08-1500:00:00\"}},{\"ID\":\"2\",\"Name\":\"Krystal\",\"PhoneNo\":\"0010000114250\",\"Data\":{\"Access\":\"23466876\",\"StartTime\":\"2023-08-1700:00:00\"}}]}";
Sample myData = JsonConvert.DeserializeObject<Sample>(json);
Console.WriteLine("Status="+myData.Status.ToString()); Console.WriteLine("Result="+myData.Result.ToString());
for (int i=0;i<=myData.List.Count-1;i++)
{
Console.WriteLine("ID="+myData.List[i].ID); Console.WriteLine("Name="+myData.List[i].Name); Console.WriteLine("PhoneNo="+myData.List[i].PhoneNo);
}
}
}
public class Sample
{
public string Status { get; set; }
public result Result { get; set; }
public List<SampleList> List { get; set; }
}
public class result
{
public string Response { get; set; }
public int TotalRecord { get; set; }
}
public class SampleList
{
public string ID { get; set; }
public string Name { get; set; }
public string PhoneNo { get; set; }
public SampleListData Data { get; set; }
}
public class SampleListData
{
public string Access { get; set; }
public string StartTime { get; set; }
}
轉為 VB.Net
Imports System.Collections.Generic
Imports Newtonsoft.Json
Public Class Program
Public Shared Sub Main()
Dim json = "{""Status"":""1"",""Result"":{""Response"":""Ok"",""TotalRecord"":2},""List"":[{""ID"":""1"",""Name"":""Karen"",""PhoneNo"":""0010000114248"",""Data"":{""Access"":""123456789"",""StartTime"":""2023-08-1500:00:00""}},{""ID"":""2"",""Name"":""Krystal"",""PhoneNo"":""0010000114250"",""Data"":{""Access"":""23466876"",""StartTime"":""2023-08-1700:00:00""}}]}"
Dim myData As Sample = JsonConvert.DeserializeObject(Of Sample)(json)
Console.WriteLine("Status=" & myData.Status.ToString())
Console.WriteLine("Result=" & myData.Result.ToString())
For i = 0 To myData.List.Count - 1
Console.WriteLine("ID=" & myData.List(i).ID)
Console.WriteLine("Name=" & myData.List(i).Name)
Console.WriteLine("PhoneNo=" & myData.List(i).PhoneNo)
Next
End Sub
End Class
Public Class Sample
Public Property Status As String
Public Property Result As result
Public Property List As List(Of SampleList)
End Class
Public Class result
Public Property Response As String
Public Property TotalRecord As Integer
End Class
Public Class SampleList
Public Property ID As String
Public Property Name As String
Public Property PhoneNo As String
Public Property Data As SampleListData
End Class
Public Class SampleListData
Public Property Access As String
Public Property StartTime As String
End Class
我測試了一下~少加這一句話~
DataStr = Newtonsoft.Json.Linq.JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("Data")))
測試頁~
<%@ Page Title="" Language="VB" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rte As String = ""
rte &= " {"
rte &= " ""Status"": ""1"","
rte &= " ""Result"": {"
rte &= " ""Response"": ""Ok"","
rte &= " ""TotalRecord"": 2"
rte &= " },"
rte &= " ""List"": ["
rte &= " {"
rte &= " ""ID"": ""1"","
rte &= " ""Name"": ""Karen"","
rte &= " ""PhoneNo"": ""0010000114248"","
rte &= " ""Data"":{"
rte &= " ""Access"": ""123456789"","
rte &= " ""StartTime"": ""2023-08-15 00:00:00"""
rte &= " }"
rte &= " },"
rte &= " {"
rte &= " ""ID"": ""2"","
rte &= " ""Name"": ""Krystal"","
rte &= " ""PhoneNo"": ""0010000114250"","
rte &= " ""Data"":{"
rte &= " ""Access"": ""23466876"","
rte &= " ""StartTime"": ""2023-08-17 00:00:00"""
rte &= " }"
rte &= " }"
rte &= " ]"
rte &= " }"
Dim Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(rte)
Dim ListArray As String = Newtonsoft.Json.JsonConvert.SerializeObject(Obj.Item("List"))
Dim dt As System.Data.DataTable = New System.Data.DataTable("Result")
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("phoneNo")
dt.Columns.Add("access")
dt.Columns.Add("startTime")
Dim DataStr As Newtonsoft.Json.Linq.JObject
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")), """", "")
Dim access As String = ""
Dim startTime As String = ""
Try
DataStr = Newtonsoft.Json.Linq.JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject(j).Item("Data")))
access = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("Access")), """", "")
startTime = Replace(Newtonsoft.Json.JsonConvert.SerializeObject(DataStr.Item("StartTime")), """", "")
Catch ex As Exception
Response.Write("<br />" & ex.ToString)
End Try
dt.Rows.Add(id, name, phoneNo, access, startTime)
Next
End Sub
</script>
test..
allenlwh 恩~那要reborn看的懂並應用吧~
畢竟他寫VB語法的~跟C#有差別~
VB語法採用直覺式寫法~跟PHP是一樣的方式~@@
所以習慣寫C#語法的人~看不懂VB是正常的~
純真的人 我有另提供VB語法
reborn
另外~這個Json轉換class或DataSet 或者 Class或DataSet轉換Json
教學網址可以參考一下~
https://dotblogs.com.tw/berrynote/2016/08/18/200338
ok 謝謝你們