|
-
Nov 16th, 2017, 12:11 PM
#1
Thread Starter
Member
[RESOLVED] JSON parse/token issue
I have a basic understanding of JSON, but clearly not enough.
I am able to create a messagebox with "ID" from this JSON
Code:
{
"profileIconId": 0000,
"name": "name",
"id": 12345678,
}
with the following vb.net code
Code:
Dim json As JObject = JObject.Parse(returnData)
MsgBox(json.SelectToken("id"))
That's the easy part.
I am unable to display "tier" because there are two tokens by the name "tier". Here's the JSON (stripped down for easier reading)
Code:
[
{
"queueType": "RANKED_FLEX_SR",
"rank": "III",
"tier": "BRONZE",
"leaguePoints": 5
},
{
"queueType": "RANKED_SOLO_5x5",
"rank": "III",
"tier": "SILVER",
"leaguePoints": 22
}
]
I've tried the following in multiple different fasions to no avail.
Code:
Dim json As JObject = JObject.Parse(returnData)
MsgBox(json.SelectToken(1).SelectToken("tier"))
I wanted to learn how APIs worked so I went with a game I was familiar with, LoL. I've got the webrequests down (EASY MONEY) now I'm teaching myself what I can do with the information.
-
Nov 16th, 2017, 12:56 PM
#2
Re: JSON parse/token issue
I'm assuming you're using Newtonsoft JSON? It's nice to name the library you're using.
Most people don't use JObject.Parse() directly, it's a little clunkier than the kind of serialization JSON was designed for. I don't think you're using your query properly, though. For example:
Code:
json.SelectToken(1)
SelectToken() is:
Code:
Function SelectToken(path As String) As JToken
'path' is a "JPath expression" according to documentation, which I think meant to say "JSONPath". JSONPath is very much like XPath, but uses an object notiation. "1" is not valid JPath, this isn't an array.
I think to get what you want your code would look more like:
Code:
json.SelectToken("$[1].tier")
I'm not 100% sure there. It's "bad form" in JSON to return a raw array like that. Most developers would rather:
Code:
{ "queues" : [
{
...
},
{
...
}
]
}
That would let you grab the 2nd tier like:
Code:
json.SelectToken("$.queues[1].tier")
But this is not how I would parse this JSON. Instead, I would make an object to represent this JSON. It is "an array of something", where "something" has the four properties in the object. So:
Code:
Public Module Example
Public Sub Main()
Dim json = (get the string for your JSON)
Dim items() As ExampleData = JsonConvert.DeserializeObject(Of ExampleData())(json)
Console.WriteLine(items(1).Tier)
End Sub
End Module
Public Class ExampleData
Public Property QueueType As String
Public Property Rank As String
Public Property Tier As String
Public Property LeaguePoints As String
End Class
That will probably do the trick, and messing with objects is much easier than messing with JObject and the other parser infrastructure.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 16th, 2017, 01:04 PM
#3
Re: JSON parse/token issue
Firstly I couldn't get the code you were using to work with the sample json, I needed to use JArray.Parse rather than JObject.Parse. However once it did that it was a matter of treating the variable json as an array, not as a json object.
So
Code:
Dim json = JArray.Parse(returnData)
Dim res = json(1).SelectToken("tier")
did the trick. I am not sure if this is likely to help as it depends on just how much you simplified the example data in question.
Personally I tend to avoid using the low level JObject etc. methods and create a class that defines the data structure I am working with and then have it deserialise straight into the object.
https://www.newtonsoft.com/json/help...lizeObject.htm has a quick example of how it would work, you would just need to define the class structure although something like https://jsonutils.com/ might be able to do that for you.
Edit : Too slow on the typing, beaten to it again!
-
Nov 16th, 2017, 01:27 PM
#4
Thread Starter
Member
Re: JSON parse/token issue
Worked great! Thank you for the time and effort!
-
Nov 16th, 2017, 01:28 PM
#5
Thread Starter
Member
Re: JSON parse/token issue
sorry about the library, slipped my mind. thank you for all the good read! learned a lot.
-
Nov 17th, 2017, 02:41 PM
#6
Thread Starter
Member
Re: JSON parse/token issue
Code:
Private Sub GetRank2()
Try
Dim Request As WebRequest = WebRequest.Create("https://na1.api.riotgames.com/lol/league/v3/positions/by-summoner/" & SummonerID & "?api_key=" & APIKey)
Request.Credentials = CredentialCache.DefaultCredentials
Request.Proxy = Nothing
Dim Response As WebResponse = Request.GetResponse()
Dim ResponseStream As System.IO.Stream = Response.GetResponseStream
Dim StreamReader As New System.IO.StreamReader(ResponseStream)
Dim returnData As String = StreamReader.ReadToEnd
StreamReader.Close()
Dim json = returnData
Dim items() As GivePropClass = JsonConvert.DeserializeObject(Of GivePropClass())(json)
Catch ex As Exception
End Try
End Sub
I have the class seperatly as GivePropClass. It's having an issue with "jsonconvert" (as italicized above)
-
Nov 17th, 2017, 04:13 PM
#7
Re: [RESOLVED] JSON parse/token issue
Try two things.
- Take a deep breath, facepalm, and put "Imports Newtonsoft.Json" at the top of your file. (A lot of times if you right-click, choose "Quick fixes" or whatever, it will suggest this.)
- Describe "having an issue", because I can't see your computer screen. You'll probably have to post the implementation of "GivePropClass" too.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 17th, 2017, 05:16 PM
#8
Thread Starter
Member
Re: [RESOLVED] JSON parse/token issue
Holy moly..did you feel the facepalm? I had Newtonsoft.Json.Linq, it never occured to me I'd need to import the rest.
I guess the following isn't correct? (This is my first time using Classes)
Code:
Public Class GivePropClass
Public Property QueueType As String
Public Property Rank As String
Public Property Tier As String
Public Property LeaguePoints As String
End Class
Private Sub GetRank2()
Try
Dim Request As WebRequest = WebRequest.Create("https://na1.api.riotgames.com/lol/league/v3/positions/by-summoner/" & SummonerID & "?api_key=" & APIKey)
Request.Credentials = CredentialCache.DefaultCredentials
Request.Proxy = Nothing
Dim Response As WebResponse = Request.GetResponse()
Dim ResponseStream As System.IO.Stream = Response.GetResponseStream
Dim StreamReader As New System.IO.StreamReader(ResponseStream)
Dim returnData As String = StreamReader.ReadToEnd
StreamReader.Close()
Dim json = returnData
Dim items() As GivePropClass = JsonConvert.DeserializeObject(Of GivePropClass())(json)
MsgBox(PositionData.Tier.ToString)
Catch ex As Exception
MsgBox("ERROR")
End Try
End Sub
I have Dim PositionData As GivePropClass in my main class.
Thank you for your patience!
Last edited by jj103; Nov 17th, 2017 at 07:21 PM.
Reason: More deails.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|