This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Martin2789's profile picture

JSON API usage

Started by Martin2789, 12 February 2014 - 01:30 PM
Martin2789 #1
Posted 12 February 2014 - 02:30 PM
Repost from the JSON-Api Thread. I would like to read a JSON File which has more layers :
Nice Api you have there i get it to work with easy json like:

		{
			"firstline": "John",
			"secline": "Doe",
			"txtcol": "colors.white",
			"backcol": "colors.blue"
		}
with

os.loadAPI("json")
str = http.get("http://www.someserver.com/").readAll()
obj = json.decode(str)
print(obj.firstline)
but with complicated json like:


{
	"monitornorth": [
		{
			"firstline": "John",
			"secline": "Doe",
			"txtcol": "colors.white",
			"backcol": "colors.blue"
		}
	]
}


os.loadAPI("json")
str = http.get("http://www.someserver.com/").readAll()
obj = json.decode(str)
print(obj.monitornorth.firstline)
obj.monitornorth.firstline is empty
What do i do wrong?
LBPHacker #2
Posted 13 February 2014 - 12:51 PM

{
	"monitornorth": [
		{
			"firstline": "John",
			"secline": "Doe",
			"txtcol": "colors.white",
			"backcol": "colors.blue"
		}
	]
}
Take a look at that piece of JSON again. monitornorth is an array with one object in it. That object is the first thing in monitornorth, so this is how you access it:
print(obj.monitornorth[1].firstline)
Martin2789 #3
Posted 14 February 2014 - 11:18 AM
Take a look at that piece of JSON again. monitornorth is an array with one object in it. That object is the first thing in monitornorth, so this is how you access it:
print(obj.monitornorth[1].firstline)

Thank you very much… now it is clear why it didn't worked Thank you very very much!!!