Good day,
I need to get some values from a JSON file, but I can not deserialize the file.
It is available here:http://files.minecraftforge.net/maven/net/minecraftforge/forge/json
What I would like to get are the values of "mcversion" and "version" found in the "number" section, the remaining data not interest me.
I generated classes but do not work due to JSON format, this special is different.
I hope you can help me, thank you very much.
To Read from a File you can use
Dim json As String = File.ReadAllText("JSON File Path")
Dim o As JObject = JObject.Parse(json)
Related
Lot of similar questions but still not able to make it. Here is my
code in vb.net and this is the json response. enter image description here I know this is because of [] but i'm using list don't know still getting the same error.
This is because you are receiving a JSON array and not a single JSON object.
You need to deserialize the array like this -
Dim Items_Array = Newtonsoft.Json.JsonConvert.DeserializeObject(Of T())(jsonString)
I'm preparing some kind of software working with Excel with an API, and I'm using vba-json library, and I dont know how to get data from nested arrays on JSON.
I've tryied some tutorials and another similar questions that I fond here. But every time I try I get a different error. Runtime error 5, 9, 13. Tryied different ways to accés the data but everytime I get error when I get into an array.
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", "https://pokeapi.co/api/v2/pokemon/ditto/?fbclid=IwAR2pJwAgODOlI-Gdn8pH-RDFCcUfQWiYLZIVCnP8e-V_9gEwYymqRldpiFk"
MyRequest.Send
Dim Json As Object
Set Json = JsonConverter.ParseJson(MyRequest.ResponseText)
MsgBox Json("stats")("id")(0)("base_stat")
I'd like to get the data from the selected array to later be able to work with it, for example from
("stats")("id")(0)("base_stat")
get
48
The Json source is on the code.
So, with JSON the [] denotes a collection you can For Each over and access by index, the {} indicates dictionaries you can For Each the dict keys of, or access items by key.
You can get a feel for the structure by pasting it into a json viewer such as this. You will need to familiarise yourself with reading json but you will see that, for that value you want, it has a path of:
json("stats")(1)("base_stat")
Note: The indexing starts at one for collections in VBA JSON though it is displays 0 in the viewer.
Reading the structure:
Option Explicit
Public Sub test()
Dim Json As Object
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", "https://pokeapi.co/api/v2/pokemon/ditto/?fbclid=IwAR2pJwAgODOlI-Gdn8pH-RDFCcUfQWiYLZIVCnP8e-V_9gEwYymqRldpiFk"
.send
Set Json = JsonConverter.ParseJson(.responseText)
End With
MsgBox Json("stats")(1)("base_stat")
End Sub
Note there are various answers on StackOverflow which provide code that will list the access paths to every item in a json structure. In particular, I remember this great answer from #tinman here. It's not a substitute for learning to read JSON but an excellent tool for aiding and checking your understanding.
I have my HttpWebRequest body that needs to be in JSON array format and I have to populate it dynamically at run time.
Dim body As String = "{""studentForm"":[{""title"":""Law Major"",""description"":""Excellent Student. Work hard. Very Intelligent, and so on..."",""phone"":""0123456789"",""categoryId"":1197,""categoryName"":""Senior Student"",""parentCategory"":9,""location"":{""latitude"":45.151053,""longitude"":-79.398193,""radius"":1000},""categoryAttributes"":[],""imageUrls"":[]}]}"
So I need to have variables like varTitle, varDescript, varDescript, and so on into the JSON array. I tried to do that the below way but doesn't work.
Dim body As String = "{""studentForm"":[{""title"":varTitle,""description"":varDescript,""phone"":varDescript,""categoryId"":1197,""categoryName"":""Senior Student"",""parentCategory"":9,""location"":{""latitude"":45.151053,""longitude"":-79.398193,""radius"":1000},""categoryAttributes"":[],""imageUrls"":[]}]}"
I read about Deserialization / Serialization from a couple of websites but still can't achieve what I'm looking for.
I have downloaded and set the JSON parser for VB6 from this website:
VB-JSON
I cannot understand how this module works. I successfuly set it in Excel 2010 and I think I also understand the JSON format but I don't know how this class extracts the items. I tried to parsed the following text in the JSON format
{"realms":[{"type":"pvp","population":"low","queue":false,"wintergrasp":{"area":1,"controlling-faction":1,"status":0,"next":1356724174636},"tol-barad":{"area":21,"controlling-faction":1,"status":0,"next":1356723246779},"status":true,"name":"Kor'gall","slug":"korgall","battlegroup":"Cruelty / Crueldad","locale":"en_GB","timezone":"Europe/Paris"},{"type":"pve","population":"medium","queue":false,"wintergrasp":{"area":1,"controlling-faction":0,"status":0,"next":1356724425638},"tol-barad":{"area":21,"controlling-faction":0,"status":0,"next":1356723369780},"status":true,"name":"Alonsus","slug":"alonsus","battlegroup":"Cruelty / Crueldad","locale":"en_GB","timezone":"Europe/Paris"}]}
A browser will return this content when the following address is used:
Alonsus, Kor'gall
I wrote a code similar to this
Dim objJSON As Object
...
strData = objJSON.Item("Realms")(1).Item("Type")
but it causes errors: "Object variable or With block variable not set". I expected to get "pve" value. I am confused because it starts with an object name "realms" followed by an Array. The array is supposed to be returned as a collection. I would be grateful for any help with that.
Thanks
set objJSON = JSON.Parse(jsonFromUrl)
strData = objJSON.Item("realms").Item(1).Item("type")
Keys inside json is case-sensitive.
I would like to use Microsoft ScriptControl to parse a JSON string in VBA, and then transform the resulting Object into Dictionary and Collection objects. I already know how to do the parsing with ScriptControl, but cannot figure out how to map the result into the Dictionary and Collection classes. I'm guessing that if I could figure out how to loop through the properties of an Object this would become clear...
Dim sc As ScriptControl
Dim obj As Variant
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
Set obj = sc.Eval("("+json+")") ' json is a string containing raw JSON
' Now what?
By the way, I've used the vba-json library to get the output in terms of Dictionaries and Collections, but I find this library somewhat slow. It does not use ScriptControl.
EDIT: I found a discussion of getting object properties in this post.
Using JavaScript features of parsing JSON, on top of ScriptControl, we can create a parser in VBA which will list each and every data point inside the JSON. No matter how nested or complex the data structure is, as long as we provide a valid JSON, this parser will return a complete tree structure.
JavaScript’s Eval, getKeys and getProperty methods provide building blocks for validating and reading JSON.
Coupled with a recursive function in VBA we can iterate through all the keys (up to nth level) in a JSON string. Then using a Tree control (used in this article) or a dictionary or even on a simple worksheet, we can arrange the JSON data as required.
VBA Code:http://ashuvba.blogspot.in/2014/09/json-parser-in-vba-browsing-through-net.html
loop
This will help you to loop - add a myitem(n) method in javascript
from there you can map through VB code.