How do I parse through JSON?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
zhangyouwen
Posts: 11
Joined: Tue Mar 31, 2015 2:14 pm

How do I parse through JSON?

Post by zhangyouwen » Fri May 01, 2015 4:10 pm

I am trying to make a weather app using a API provided by http://www.weather.com.cn

I have three elements on my stack, "weatherdata" field, "city"field and a button. The idea is there is a "cities.txt"file saved in the desktop stored the city name with its corresponding code that is identical to the API. Something like this:
101010100=Beijing
101010300=Chaoyang
101010400=Shuangyi
101010500=Huairou
101010600=Tongzhou
101010700=Changping
101010800=Yanqing


the API is a link: http://m.weather.com.cn/data/ (city code.html) For example if I want the weather info of Beijng the link would be look like:http://m.weather.com.cn/data/101110101.html

when get into the link, it will provide the weather data in JSON format like:

Code: Select all

{"weatherinfo":{"city":"西安","city_en":"xian","date_y":"2014年3月4日","date":"","week":"星期二","fchh":"11","cityid":"101110101","temp1":"13℃~3℃"
And I want my app to gather the info from that, only appear the specific information in corresponding field.

Basically, what I want is when I type Beijing in city field and click the button, the app will replace the (city code.html)part to corresponding city code form "cities.txt". And then the "weatherdata" field will display the information provided by the API in a format that we could read.

So far I copied EasyJSON https://github.com/luxlogica/easyjson into my StackScript. And my button code looks like this:

Code: Select all

on mouseUp
   put the text of fld "city" into tCityName
   put specialFolderPath("desktop") & "/cities.txt" into tFilePath

   put textDecode(tCityList,"UTF8") into tCityList 
   put lineOffset("=" & tCityName & cr,tCityList & cr) into tFoundLine
   set the itemDelimiter to "="
   put item 1 of line tFoundLine of tCityList into tCityCode

   put "http://m.weather.com.cn/data/" & tCityCode & ".html" into tURL
   put URL tURL into tRawJSON
   put textDecode(tRawJSON,"UTF8") into fld "weatherdata"
end mouseUp
However, it is not working, except in the weather data field this showed up:

Code: Select all

<html>
<head>
</head>
<body>
<script type="text/javascript">
	window.onload = function() {
	window.open("/","_self");
	}; 
	</script> 
 <!-- START WRating v1.0 -->
<script type="text/javascript" src="http://c.wrating.com/a1.js">
</script>
<script type="text/javascript">
var vjAcc="860010-2151010100";
var wrUrl="http://c.wrating.com/";
vjTrack("");
</script>
<noscript><img src="http://c.wrating.com/a.gif?a=&c=860010-2151010100" width="1" height="1"/></noscript>
<!-- END WRating v1.0 -->
</body>
</html>
Can anyone help me out to realize this idea? I've uploaded "cities.txt" and the app to dropbox, please take a look:https://www.dropbox.com/s/9dbttvmbg0e4v ... p.zip?dl=0

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: How do I parse through JSON?

Post by bangkok » Wed May 06, 2015 6:10 pm

[quote="zhangyouwen"]
So far I copied EasyJSON https://github.com/luxlogica/easyjson into my StackScript. And my button code looks like this:

Yes EasyJSON is a good one.

Here is what you need to do :

on mouseUp
put the text of fld "city" into tCityName
put specialFolderPath("desktop") & "/cities.txt" into tFilePath

put textDecode(tCityList,"UTF8") into tCityList
put lineOffset("=" & tCityName & cr,tCityList & cr) into tFoundLine
set the itemDelimiter to "="
put item 1 of line tFoundLine of tCityList into tCityCode

put "http://m.weather.com.cn/data/" & tCityCode & ".html" into tURL
put URL tURL into tRawJSON

put arrayFromJson(tRawJSON) into tArray

end mouseUp


But watch out ! You made a mistake with your cities.txt file.

This file is in... RTF format.

Not good (for proper parsing)

Use instead raw .txt

I've tested with the code for Beijng : 101010100

It's working fine.

You will get in tArray all the proper data, as an array.

zhangyouwen
Posts: 11
Joined: Tue Mar 31, 2015 2:14 pm

Re: How do I parse through JSON?

Post by zhangyouwen » Wed May 06, 2015 7:36 pm

Great! Thank you so much. But how do I pull out the information I need into corresponding field? For example

Code: Select all

{"weatherinfo":{"city":"霞云岭","date_y":"2009年7月27日","date":"六月初六","week":"星期一","fchh":"08","cityid":"101012100","temp1":"24℃~17℃","temp2":"25℃~18℃","temp3":"28℃~16℃","temp4":"27℃~23℃","weather1":"多云转小雨","weather2":"小雨转多云","weather3":"多云","weather4":"小雨转特大暴雨",
If I want to put city name 霞云岭 from ["city":"霞云岭"] into a "city" field when I clicked the button how can I realize that so that when I change to another city the name will change with it?

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: How do I parse through JSON?

Post by bangkok » Thu May 07, 2015 3:55 am

Once you receive the array, you have to go through the array.

Change your script :

Code: Select all

   put textDecode(tRawJSON,"UTF8") into tRawJSON
   put arrayFromJson(tRawJSON) into tArray
answer tArray["weatherinfo"]["city_en"]
answer tArray["weatherinfo"]["date_y"]
Et voilà ! :D

To know all the keys of the array your received (and there are a lot), you can use a very simple trick.

add a breakpoint a the end of the script :

Code: Select all

breakpoint
it will open the debugger, where you can see all the content of the array and its keys.

zhangyouwen
Posts: 11
Joined: Tue Mar 31, 2015 2:14 pm

Re: How do I parse through JSON?

Post by zhangyouwen » Fri May 22, 2015 4:09 pm

Hi bangkok, thank you for answering my question. I am occurring another problem with coding hope you will help me out. I switched my API to a much better one and it will return information in English.Also, this API does not need country code to identified location any more instead just simply put city name after and it will identified the location by itself. In my program, everything is fine except I don't know how to capture the information return by the API and put them into corresponding field. I've tried the same method that you told at your last response. However, it is not working.Please help me.

Here is my updated program:https://www.dropbox.com/s/5yu1svt3pwmmy ... ecode?dl=0

Thank you very much!
bangkok wrote:Once you receive the array, you have to go through the array.

Change your script :

Code: Select all

   put textDecode(tRawJSON,"UTF8") into tRawJSON
   put arrayFromJson(tRawJSON) into tArray
answer tArray["weatherinfo"]["city_en"]
answer tArray["weatherinfo"]["date_y"]
Et voilà ! :D

To know all the keys of the array your received (and there are a lot), you can use a very simple trick.

add a breakpoint a the end of the script :

Code: Select all

breakpoint
it will open the debugger, where you can see all the content of the array and its keys.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: How do I parse through JSON?

Post by bangkok » Sun May 24, 2015 10:32 am

It's working perfectly well.

You just don't make enough effort to understand how arrays are working (sorry for my blunt language)

Code: Select all

   put tArray["weatherinfo"]["temp"] into fld "Temp"
doesn't work, because none of these are keys in the new API.

use instead :

Code: Select all

   put tArray["weather"][1]["description"] into fld "Temp"
or

Code: Select all

   put tArray["wind"]["speed"] into fld "Temp"
I told you : use the debugger to check the content of the array, and to see all the keys of the array.

Post Reply