Comments (9)
Hi @lbhack !
Here is a small demo of how to correctly parse the JSON of your example.
/* NAppGUI Console Application */
#include "coreall.h"
#include "httpreq.h"
#include "json.h"
#include "inet.h"
/* Your C structs MUST follow the same organization as JSON, for automatic parsing. */
typedef struct _condition_t Condition;
typedef struct _weather_t Weather;
/* Every Condition has several fields, but we are only interested in 'temp_C' what is of string type */
struct _condition_t
{
String *temp_C;
};
/* SetUp arrays and sets for Condition structure */
DeclSt(Condition);
/* The global JSON has an ARRAY of objects of type Condition called 'current_condition' */
struct _weather_t
{
ArrSt(Condition) *current_condition;
};
/*---------------------------------------------------------------------------*/
/* Data binding ONLY once when application starts */
/* https://nappgui.com/en/core/dbind.html */
/* https://nappgui.com/en/inet/json.html */
static void i_dbind(void)
{
dbind(Condition, String*, temp_C);
dbind(Weather, ArrSt(Condition)*, current_condition);
}
/*---------------------------------------------------------------------------*/
static Weather *i_get_weather(void)
{
Weather *weather = NULL;
Http *ht = http_secure("wttr.in",443);
if(http_get(ht, "/madrid?format=j1", NULL, 0, NULL) == TRUE)
{
Stream *gdata = stm_memory(4096);
if (http_response_body(ht, gdata, NULL) == TRUE)
{
/* We have the JSON string --> Parse to C Struct */
weather = json_read(gdata, NULL, Weather);
}
/* ALWAYS you have to destroy the stream (not only if respose fails) */
stm_close(&gdata);
}
http_destroy(&ht);
return weather;
}
/*---------------------------------------------------------------------------*/
/* Console application created with */
/* commandApp("SimpleJson" "simplejson" "osapp;inet" NRC_PACKED) */
int main(int argc, char *argv[])
{
uint32_t i = 0;
unref(argc);
unref(argv);
core_start();
inet_start();
/* First, data binding, just once */
i_dbind();
for(i = 0; i < 10; ++i)
{
Weather *weather = i_get_weather();
/* Weather can be obtained from REST-API */
if (weather != NULL)
{
if(weather->current_condition != NULL)
{
/* We are interested in first element of 'current_condition' */
if(arrst_size(weather->current_condition, Condition) > 0)
{
const Condition *condition = arrst_first_const(weather->current_condition, Condition);
bstd_printf("temp_C: %s\n", tc(condition->temp_C));
}
}
/* Data Binding knows how to completely destroy a binded object */
/* https://nappgui.com/en/core/dbind.html#h4 */
dbind_destroy(&weather, Weather);
}
}
core_finish();
inet_finish();
return 0;
}
temp_C: 2
temp_C: 2
temp_C: 2
temp_C: 2
temp_C: 2
temp_C: 2
temp_C: 2
temp_C: 2
temp_C: 2
temp_C: 2
[23:20:21] [OK] Heap Memory Staticstics
[23:20:21] ============================
[23:20:21] Total a/dellocations: 273, 273
[23:20:21] Total bytes a/dellocated: 1300184, 1300184
[23:20:21] Max bytes allocated: 108676
[23:20:21] Effective reallocations: (0/0)
[23:20:21] Real allocations: 2 pages of 65536 bytes
[23:20:21] 10 pages greater than 65536 bytes
[23:20:21] ============================
[23:20:21] Config: Debug
from nappgui_src.
mucha gracias
from nappgui_src.
from nappgui_src.
I'm sure you can do it yourself, following the example I gave you.
from nappgui_src.
ok thanks i get it .
from nappgui_src.
here i finished the mini project with code about Weather Information :
/*
- NAppGUI Cross-platform C SDK
- 2015-2022 jose jaramillo
- MIT Licence
- File: main.c
*/
/* NAppGUI Weather Info */
#include "nappgui.h"
#include "inet.h"
#include "httpreq.h"
#include "json.h"
/* Define App struct, which contains the main window, text view, and other elements /
typedef struct _app_t App;
/ Your C structs MUST follow the same organization as JSON, for automatic parsing. /
typedef struct _condition_t Condition;
typedef struct _weather_t Weather;
typedef struct val Values;
typedef struct nearest_area Nearestarea;
typedef struct count Country;
/ Every Condition has several fields, but we are only interested in 'temp_C' what is of string type */
struct count
{
String *value; //value of country
};
struct val
{
String *value; // value Weather description
};
struct _condition_t
{
String *pressure;
String *temp_F;
String *temp_C;
ArrSt(Values) *weatherDesc;
};
struct nearest_area
{
ArrSt(Country) *country;
};
/* SetUp arrays and sets for Condition structure /
DeclSt(Condition);
DeclSt(Values);
DeclSt(Nearestarea);
DeclSt(Country);
/ The global JSON has an ARRAY of objects of type Condition called 'current_condition' */
struct _weather_t
{
// ArrSt(Condition) *nearest_area;
ArrSt(Condition) *current_condition;
ArrSt(Nearestarea) *nearest_area;
};
struct _app_t
{
Window *window;
TextView *text;
String *lop;
String count;
Edit country;
};
/---------------------------------------------------------------------------/
static void init_bind()
{
dbind(Condition, String*, temp_C);
dbind(Condition, String*, pressure);
dbind(Condition, String*, temp_F);
dbind(Values, String*, value);
dbind(Condition, ArrSt(Values)* , weatherDesc);
dbind(Weather, ArrSt(Condition)*, current_condition);
dbind(Country, String*, value);
dbind(Nearestarea, ArrSt(Country)*, country);
dbind(Weather, ArrSt(Nearestarea)*, nearest_area);
/*---------------------------------------------------------------------------*/
}
static uint32_t i_OnButton(App *app)
{
Stream *gdata = NULL;
Weather *weather = NULL;;
String *path;
Http *ht = http_secure("wttr.in",443);
path = str_printf("/%s?format=j1",app->count);
if(http_get(ht, tc(path), NULL, 0, NULL) == TRUE)
{
gdata = stm_memory(4096);
if (http_response_body(ht, gdata, NULL) == FALSE)
stm_close(&gdata);
}
http_destroy(&ht);
weather = json_read(gdata,NULL, Weather);
stm_close(&gdata);
/* Weather can be obtained from REST-API */
if (weather != NULL)
{
if(weather->current_condition != NULL)
{
if(arrst_size(weather->current_condition,Condition) > 0)
{
const Condition *condition = arrst_first_const(weather->current_condition, Condition);
const Values *weatherDesc2 = arrst_first_const(condition->weatherDesc, Values);
const Nearestarea *near = arrst_first_const(weather->nearest_area,Nearestarea);
const Country *counname = arrst_first_const(near->country, Country);
app->lop = str_printf("\n City name : %s\n\n --------------- \n\n pressure : %s \n temp_C : %s \n temp_F : %s \n weatherDesc: %s \n Country : %s \n ---------------- ",app->count, tc(condition->pressure),tc(condition->temp_C),tc(condition->temp_F),tc(weatherDesc2->value),tc(counname->value));
}
}
dbind_destroy(&weather, Weather);
}
return 0;
}
static void end_t(App *app,uint32_t rvalue)
{
if (rvalue == 0)
{
textview_printf(app->text,"%s\n",app->lop->data);
edit_text(app->country,"");
textview_scroll_down(app->text);
str_destroy(&app->lop);
}
}
static void bt(App *app,Event *e)
{
textview_clear(app->text);
app->count = edit_get_text(app->country);
osapp_task(app,0,i_OnButton,NULL,end_t,App);
unref(e);
}
static void ex(App *app,Event *e)
{
osapp_finish();
unref(app);
unref(e);
}
static Panel *i_panel(App *app)
{
Panel *panel = panel_create();
Layout *layout = layout_create(1, 5);
Label *desc = label_create();
Button *button = button_push();
Button *Exit = button_push();
TextView *text = textview_create();
app->country = edit_create();
app->text = text;
button_text(button, "Get Weather");
button_text(Exit, "Exit");
label_text(desc, "Enter Country or City Then Press Enter :");
button_OnClick(button, listener(app, bt, App));
button_OnClick(Exit, listener(app, ex, App));
textview_color(app->text, kCOLOR_GREEN);
textview_halign(app->text, ekCENTER);
textview_size(app->text, s2df(30.0, 30.0));
label_color(desc,kCOLOR_YELLOW);
layout_label(layout, desc, 0, 0);
layout_edit(layout, app->country, 0, 1);
layout_button(layout, button, 0, 2);
layout_button(layout, Exit, 0, 3);
layout_textview(layout, text, 0, 4);
layout_hsize(layout, 0, 250);
layout_vsize(layout, 4, 200);
layout_margin(layout, 5);
layout_vmargin(layout, 0, 5);
layout_vmargin(layout, 2, 5);
layout_vmargin(layout, 2, 5);
layout_vmargin(layout, 1, 5);
panel_layout(panel, layout);
init_bind();
return panel;
}
/---------------------------------------------------------------------------/
static void i_OnClose(App *app, Event *e)
{
bool_t *result = event_result(e, bool_t);
*result = FALSE;
bt(app,ekFOK);
unref(app);
unref(e);
}
/---------------------------------------------------------------------------/
static App *i_create(void)
{
App *app = heap_new0(App);
Panel *panel = i_panel(app);
app->window = window_create(ekWINDOW_TITLE | ekWINDOW_RETURN);
window_panel(app->window, panel);
window_title(app->window, "Weather info");
window_origin(app->window, v2df(500, 200));
window_OnClose(app->window, listener(app, i_OnClose, App));
window_show(app->window);
return app;
}
/---------------------------------------------------------------------------/
static void i_destroy(App **app)
{
window_destroy(&(*app)->window);
heap_delete(app, App);
}
/---------------------------------------------------------------------------/
#include "osmain.h"
osmain(i_create, i_destroy, "", App)
from nappgui_src.
from nappgui_src.
Hi @lbhack !
That's looks good!
Congratulations on completing your first NAppGUI project.
from nappgui_src.
Improved Json documentation:
https://nappgui.com/en/inet/json.html
https://nappgui.com/en/howto/htjson.html
from nappgui_src.
Related Issues (20)
- Selected item colour in listboxes and popups incorrect on Mac HOT 2
- Printing implementation HOT 2
- AlertBox or MessageBox enhancement
- CMake throws up on Windows HOT 3
- CMake warning HOT 2
- Make scroll speed of TextView configurable
- Image support enhancements HOT 5
- The main site is broken HOT 4
- Cannot clear PopUp, popup_clear(..) not working on Mac
- Bug in str_splits HOT 3
- Allow building for Windows with MinGW-w64/GCC HOT 2
- Regular expression problem HOT 1
- Bug in str_repl ? HOT 2
- Video support HOT 1
- Support for gtk4 HOT 3
- Possible bug in tableview_select(...) HOT 2
- Crash in ColorView - NULL pointer access HOT 2
- Problem with streams in core HOT 2
- Warnings aplenty with gcc HOT 8
- Customize log path and whether to output log HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nappgui_src.