PHP json_encode() -> lua json.decode()

I have what appears to be a fairly simple process: Make a request to a server, get some json back with text data in it. Decode and proceed to layout.

It should work except that when the json input is decoded by lua an empty table is returned.

Here’s the json file that is encoded on the server in PHP json_encode()

{“Time”:“19:55”,“Condition”:“Overcast”,“Wind”:“W 19/0 (22/0)”,“Temperature”:“20(69)”,“Humidity”:“71”,“Pressure”:“1013 /29.91”}

PHP’s json_encode has many options. does anybody know what the right one might be to get Corona to decode the file properly?

Has anybody else run in to this problem?
[import]uid: 11419 topic_id: 19715 reply_id: 319715[/import]

Nothing wrong with that. I tried with this code and it worked:

[lua]require(“json”);

local jsonString = ‘{“Time”:“19:55”,“Condition”:“Overcast”,“Wind”:“W 19/0 (22/0)”,“Temperature”:“20(69)”,“Humidity”:“71”,“Pressure”:“1013 /29.91”}’;
x = json.decode(jsonString);

for k,v in pairs(x) do
print(k…":"…v)
end[/lua]
output:

Wind:W 19/0 (22/0)
Time:19:55
Humidity:71
Pressure:1013 /29.91
Condition:Overcast
Temperature:20(69)

[import]uid: 70847 topic_id: 19715 reply_id: 76302[/import]

That’s interesting … and yet frustrating.

So that can only mean that I have made some glaring mistake.

Thanks for the response. I need to double-check everything and figure out what I am doing wrong.

Will post back in the unlikely event that what I find is interesting. [import]uid: 11419 topic_id: 19715 reply_id: 76310[/import]

Hi there,

Two things happened here, and I can see that Lua takes some getting used to.

Your code used pairs, mine used ipairs. Mine returned nothing. When I switched to pairs it worked. Array vs table? Yes, ipairs does not iterate over string keys.

Next I was counting the table with # table
This also doesn’t work, and is documented around and about. # only counts integer keys.

Such are the pitfalls. In PHP the array/table would be treated the same regardless of the data type of the keys.

I leave this here in the hope that it is not clutter, and that it will help someone who fell in the same hole as I did.

Thanks again for your response.

The following code is WRONG and returns only ‘0’

[code]
require(“json”);

local jsonString = ‘{“Time”:“19:55”,“Condition”:“Overcast”,“Wind”:“W 19/0 (22/0)”,“Temperature”:“20(69)”,“Humidity”:“71”,“Pressure”:“1013 /29.91”}’;
x = json.decode(jsonString);

print(#x)
for k,v in ipairs(x) do
print(k…":"…v)
end
[/code] [import]uid: 11419 topic_id: 19715 reply_id: 76504[/import]

Hello,

I did not understand the reply…

in my PHP I have this code:

$result = array();

$result[“err_code”] = 102;

$result[“message”] = “Error Message”;

json_encode($result);

Now may I know how I will call it to my LUA?

please need your help…

Hello,

I did not understand the reply…

in my PHP I have this code:

$result = array();

$result[“err_code”] = 102;

$result[“message”] = “Error Message”;

json_encode($result);

Now may I know how I will call it to my LUA?

please need your help…