Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » [SOLVED] ParseJSON weird behaviour
[SOLVED] ParseJSON weird behaviour [message #54166] Thu, 04 June 2020 23:04
forlano is currently offline  forlano
Messages: 1105
Registered: March 2006
Location: Italy
Experienced Contributor
Hello,

I have used ParseJSON in the past with success, but today I am fighting with it and I gave up after two hours of struggle.
The following program make a call to a site and get as string a JSON file. This is what I get with my current IP:

{"abbreviation":"CEST","client_ip":"151.53.2.14","datetime":"2020-06-04T22:47:26.435741+02:00","day_of_week":4,"day_of_year":156,"dst":true,"dst_from":"2020-03-29T01:00:00+00:00","dst_offset":3600,"dst_until":"2020-10-25T01:00:00+00:00","raw_offset":3600,"timezone":"Europe/Rome","unixtime":1591303646,"utc_datetime":"2020-06-04T20:47:26.435741+00:00","utc_offset":"+02:00","week_number":23}


Then I use ParseJSON to get the "utc_offset" field. This is the program:

#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
		String url = "http://worldtimeapi.org/api/ip";
		HttpRequest req(url);
		req.KeepAlive(true);
		String content = req.Execute();		
		Cout()<< content << "\n";
				
		Value js = ParseJSON(content)[0];		
	        String UTCoffset = AsString(js["utc_offset"]);
                Cout()<< UTCoffset << "\n";
	        req.Close();
}


Unfortunately UTCoffset is empty, i.e. I am not able to grab the value associated to "utc_offset". I tried several other way but no success.
What am I missing? (here is the url to test the site and get the json file http://worldtimeapi.org/api/ip );
Thanks a lot,
Luigi

EDIT: After sending the post I looked better the file format and noticed the lack of square bracket!
With this correction

Value js = ParseJSON( "["+content+"]")[0];

it works.
Sorry for the false alarm

[Updated on: Thu, 04 June 2020 23:12]

Report message to a moderator

Previous Topic: strange behaviour of Vector serialization
Next Topic: Add compilable testcases for nontrivial problems!
Goto Forum:
  


Current Time: Wed Jul 15 04:03:46 CEST 2020

Total time taken to generate the page: 0.01363 seconds