Jump to content




JSON API usage


2 replies to this topic

#1 Martin2789

  • Members
  • 5 posts

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 :

View PostMartin2789, on 11 February 2014 - 03:37 PM, said:

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?


#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 13 February 2014 - 12:51 PM

View PostMartin2789, on 12 February 2014 - 02:30 PM, said:

{
	"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)


#3 Martin2789

  • Members
  • 5 posts

Posted 14 February 2014 - 11:18 AM

View PostLBPHacker, on 13 February 2014 - 12:51 PM, said:

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!!!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users