嘿,我正试图解析以下JSON,以获得外部的颜色:
{
"styles": [{
"make": {
"id": 200001444,
.....etc etc
},
"model": {
"id": "Honda_Pilot",
.....etc etc
},
"engine": {
"id": "200058127",
.....etc etc
"rpm": {
"horsepower": 5700,
"torque": 4800
},
"valve": {
"timing": "variable valve timing",
"gear": "single overhead camshaft"
}
},
"transmission": {
"id": "200058128",
.....etc etc
},
"drivenWheels": "four wheel drive",
.....etc etc
"colors": [{
"category": "Interior",
"options": [{
"id": "200058068",
"name": "Blue",
.....etc etc
}]
}, {
"category": "Exterior",
"options": [{
"id": "200058073",
"name": "Crystal Black Pearl",
.....etc etc
}
}, {
"id": "200058072",
"name": "Polished Metal Metallic",
.....etc etc
到目前为止,我已经得到了"颜色",但我无法进一步:
VB.net代码:Dim json2
Using httpClient = New HttpClient()
json2 = Await httpClient.GetStringAsync("URL HERE")
End Using
Dim rootObject2 = JObject.Parse(json2)
For Each token As JToken In rootObject2("styles")
Dim results As List(Of JToken) = token.Children().ToList
For Each item As JProperty In results
Select Case item.Name
Case "colors"
For Each subitem As JObject In item.Values
Dim tmp = subitem("category")
For Each subitem2 As JObject In tmp.Values
Dim tmp2 = subitem("category")
Next
Next
End Select
Next
Next
我相信有一个更简单的方法来处理这个问题,但我只是想表明我在来这里获得代码帮助之前确实试图弄清楚它。
我正在尝试通过循环并获得所有外部颜色通过名称。
谢谢!
我通常使用System.Web.Script.Serialization.JsonSerializer
类实例。
在控制台(模块)应用程序中,确保您包含System.Web.Extensions
作为您的项目的引用…
定义将在反序列化过程中使用的模型。
VB。
Module Module1
Sub Main()
Dim result = GetData()
End Sub
Public Class MakeStyle
Public Property Id As Integer
End Class
Public Class ModelStyle
Public Property Id As String
End Class
Public Class EngineStyle
Public Property Id As String
Public Property RotationsPerMinute As EngineRotationsPerMinuteStyle
End Class
Public Class EngineRotationsPerMinuteStyle
Public Property HorsePower As Integer
Public Property Torque As Integer
End Class
Public Class Payload
Public Property Make As MakeStyle
Public Property Model As ModelStyle
Public Property Engine As EngineStyle
Public Property Colors As IEnumerable(Of ColorStyle)
End Class
Public Class ColorStyleOptionItem
Public Property Id As String
Public Property Name As String
End Class
Public Class ColorStyle
Public Property Category As String
Public Property Options As IEnumerable(Of ColorStyleOptionItem)
End Class
Public Class DataItem
Public Property Category As String
Public Property Id As String
Public Property Name As String
End Class
Public Function GetData() As IEnumerable(Of DataItem)
Dim payloadString As String = GetDemoData() ' json serialized data
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim payload = serializer.Deserialize(Of Payload)(payloadString) ' sets Property Values of Payload to 'Nothing' when the json fields are not present in the json string.
Dim data As IEnumerable(Of DataItem) = payload.Colors.SelectMany(Function(item) item.Options.Select(Function(opt) New DataItem With {.Category = item.Category, .Id = opt.Id, .Name = opt.Name}))
GetData = data
End Function
Public Function GetDemoData() As String
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
GetDemoData = serializer.Serialize(New Payload() With {
.Make = New MakeStyle() With {.Id = 200001444},
.Model = New ModelStyle() With {.Id = "Honda_Pilot"},
.Engine = New EngineStyle With {.Id = "200058127", .RotationsPerMinute = New EngineRotationsPerMinuteStyle() With {.HorsePower = 5700, .Torque = 4800}},
.Colors = New ColorStyle() {
New ColorStyle() With {
.Category = "Interior",
.Options = New ColorStyleOptionItem() {
New ColorStyleOptionItem() With {.Id = "200058068", .Name = "Blue"}
}
},
New ColorStyle() With {
.Category = "Exterior",
.Options = New ColorStyleOptionItem() {
New ColorStyleOptionItem() With {.Id = "200058073", .Name = "Crystal Black Pearl"},
New ColorStyleOptionItem() With {.Id = "200058072", .Name = "Polished Metal Metallic"}
}
}
}
})
End Function
End Module
我想你需要这样的东西:
For Each style As JToken In rootObject2("styles")
Dim model As String = style.SelectToken("model.id").ToString()
Console.WriteLine("Exterior colors for " & model & ":")
For Each colorCategory As JToken In style("colors")
Dim categoryName As String = colorCategory("category").ToString()
If categoryName = "Exterior" Then
For Each opt As JToken In colorCategory("options")
Dim color as String = opt("name").ToString()
Console.WriteLine(" " & color)
Next
End If
Next
Next
小提琴:https://dotnetfiddle.net/IVc2EC