编辑:
我在vb.net中获取JSON对象的价值时遇到困难。我的JSON请求发布如下所示的数据:
function submitEmail() {
var ClientsPersonalInfo = {
FullName: $("#FullName").val(),
PhoneNumber: $("#PhoneNumber").val(),
EmailAddress: $("#EmailAddress").val(),
DOB: $("#DOB").val(),
Occupation: $("#Occupation").val(),
NINumber: $("#NINumber").val(),
FullAddress: $("#FullAddress").val()
}
var ClientsData = {};
ClientsData.ClientsPersonalInfo = ClientsPersonalInfo;
var d = '{"ClientsData":' + JSON.stringify(ClientsData) + '}'
$.ajax({
type: "POST",
url: "add-new-client.aspx/SubmitEmail",
data: d,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response)
},
failure: function (msg) {
alert(msg);
}
});
}
JSON对象看起来像
{
"ClientsPersonalInfo": {
"FullName": "",
"PhoneNumber": "",
"EmailAddress": "",
"DOB": "",
"Occupation": "",
"NINumber": "",
"FullAddress": ""
}
}
上述请求返回vb.net中的对象
VB代码:
<WebMethod()> _
Public Shared Function SubmitEmail(ByVal ClientsPersonalInfo As Object) As String
// What to do next to get object "ClientsPersonalInfo"
// I want to access properties of the object like
//Dim name As String = ClientsPersonalInfo.FullName
Return "Successfully Converted."
End Function
不,我想获取此对象的值,需要附加到表中。请指导我如何获取上述对象的值?
答案 0 :(得分:1)
首先使用jsonlint确认您的Json格式有效
然后使用jsonutils
为其生成类库Public Class ClientsPersonalInfo
Public Property FullName As String
Public Property PhoneNumber As String
Public Property EmailAddress As String
Public Property DOB As String
Public Property Occupation As String
Public Property NINumber As String
Public Property FullAddress As String
End Class
Public Class ClientsVehicleInfo
Public Property DrivingLicense As String
Public Property VehicleMakeModel As String
Public Property VehicleColour As String
Public Property PolicyNumber As String
Public Property TypeOfCover As String
Public Property VehicleStoredIn As String
End Class
Public Class ClientsData
Public Property ClientsPersonalInfo As ClientsPersonalInfo
Public Property ClientsVehicleInfo As ClientsVehicleInfo
End Class
Public Class ClientData
Public Property ClientsData As ClientsData
End Class
使用Newtonsoft JSON将Json反序列化为对象,然后您可以只访问其属性值。 (记得使用Manage NuGet Packages将Json.net添加到您的项目中)
Imports Newtonsoft.Json
Dim obj = JsonConvert.DeserializeObject(Of Dictionary(Of String, ClientsData))(yourJsonString)
答案 1 :(得分:1)
至少有一个问题是没有使用Option Strict On
。错误的代码:
Shared Function SubmitEmail(ByVal ClientData As Object) As String
Dim obj = JsonConvert.DeserializeObject(Of NewClientData)(ClientData)
如果打开因Option Strict
采用字符串参数而无法编译的JsonConvert.DeserializeObject
。我不确定为什么异常(图像现在删除)似乎来自VB而不是Newtonsoft,但这没有帮助。
当方法结束时,当反序列化的对象超出范围时,它也会消失。
适用于编辑#9
提到词典的错误似乎具有误导性,可能是内部与收集属性有关的内容(很多时候json可以反序列化为Dictionary(Of String,String)。给定json发布(带数据):
{
"ClientsData": {
"ClientsPersonalInfo": {
"FullName": "Ziggy Le Strange",
"PhoneNumber": "505050",
"EmailAddress": "ziggy@foobar.com",
"DOB": "",
"Occupation": "Freelancer",
"NINumber": "7",
"FullAddress": "123 Easy street"
}
}
}
实际上有3个类:ClientsPersonalInfo
包含数据,ClientsData
是包含该数据的类,在之前的编辑中还包含ClientsVehicleInfo
类。
但是还有另一个由括号{...}
代表的类。可以为您创建课程的机器人将其命名为Example
或RootObject
。在这种情况下,我会称之为ClientContainer
。
这有效:
' the outermost {}
Public Class ClientContainer
Public Property ClientsData As ClientsData
End Class
Public Class ClientsPersonalInfo
Public Property FullName As String
Public Property PhoneNumber As String
Public Property EmailAddress As String
Public Property DOB As String
Public Property Occupation As String
Public Property NINumber As String
Public Property FullAddress As String
End Class
Public Class ClientsData
Public Property ClientsPersonalInfo As ClientsPersonalInfo
Public Property ClientsVehicleInfo As ClientsVehicleInfo
End Class
Public Class ClientsVehicleInfo
' whatever it is supposed to hold
End Class
要反序列化数据(您可能需要对其进行调整以供网络使用,Shared
似乎对我不正确):
' pass in the json AS STRING
' returns JUST the ClientsPersonalInfo
Public Function GetClientData(jsonData As String) As ClientsPersonalInfo
' you must use the container class
Dim client = JsonConvert.DeserializeObject(Of ClientContainer)(jsonData )
' TEST:
Console.WriteLine(client.ClientsData.ClientsPersonalInfo.FullName)
Return client.ClientsData.ClientsPersonalInfo
End Function
ClientsData
似乎是一个不需要的层。容器可以直接容纳两个其他对象。 如果这意味着保留多个客户的信息,您可以在json中使用密钥代替"ClientsData":
(例如"ziggy":{}, "zacky":{}, "zoey":{}
。
输出:
Ziggy Le Strange
根据评论,车辆信息是交易的一部分,您可以将其更改为返回包含个人和车辆信息的ClientsData
:
Public Function GetClientData(jsonData As String) As ClientsData
' you must use the container class
Dim client = JsonConvert.DeserializeObject(Of ClientContainer)(jsonData )
Return client.ClientsData
Option Strict
As Object
,它们会失去一些含义。另外,将Date存储为字符串看起来也很糟糕。