Re: Which newsgroup for json parsing?

Liste des GroupesRevenir à l c 
Sujet : Re: Which newsgroup for json parsing?
De : josef (at) *nospam* invalid.invalid (Josef Möllers)
Groupes : comp.lang.c
Date : 13. May 2024, 11:07:25
Autres entêtes
Message-ID : <lae3idF17r5U1@mid.individual.net>
References : 1
User-Agent : Mozilla Thunderbird
On 08.05.24 15:04, Josef Möllers wrote:
Hi all,
 I am trying to parse a json string received through MQTT from a "Shelly Plug S", e.g.
{"id":0, "source":"button", "output":true, "apower":0.0, "voltage":237.9, "current":0.000, "aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1715172119},"temperature":{"tC":41.1, "tF":106.0}}
 I am trying to use libjson-glib but I can't figure out what to use as the first argument to json_gobject_from_data()!
I am also looking at libjson-c but cannot find any examples that could guide me.
F'up to self, also to answer a few questions.
First of all, I have switched to libjson-c5 The code I'm using looks like this:
/* Requires: libjson-c-dev */
# include       <json.h>
# include       <json_tokener.h>
:
static void
xlate(unsigned int plug, unsigned int sw, const char *payload, char *item)
{
     struct json_tokener *tok;
     struct json_object *obj1, *obj2;
     tok = json_tokener_new();
     obj1 = json_tokener_parse_ex(tok, payload, strlen(payload));
     if (json_object_object_get_ex(obj1, item, &obj2) && obj2 != NULL)
     {
         json_bool b;
         enum json_type t;
         switch (t = json_object_get_type(obj2))
         {
         case json_type_boolean:
             b =  json_object_get_boolean(obj2);
:
Anton referred to the many sites that have documentation.
Yes, you're absolutely right. There are many sites that have documentation, but I found that they were not easy to spot. It usually depends upon my state of mind AND the correct queries to send to Aunt Google. I am usually glad for some code examples that help me up the first step and where I can build upon. I have myself written a few magazine articles and a lot of internal documentation for my employers on various programming and architectural topics that aim at people who start working with new-ish hardware, eg microcontrollers to get at least a square wave output on a pin or some "Hello, World!" on a UART.
Malcolm wrote:
 > JSON parsing is the sort of thing you can easily do in C, but you
 > really don't want to do yourself, becuase it is a lot of fiddly code
 > to write, and everyone wants to solve exactly the same problem, which
 > is to query text formatted as JSON, usually for fields whose names and
 > types are known, but with facilities to list fields onthe fly.
Yes, that was exactly my idea when writing the program: why come up with something new when there is existing code to build upon.
To answer jak's question:
This is code solely for myself to translate a json string I receive from a Shelly Plug S into separate MQTT topics for the various items in the string, most important "output", but also "voltage" and "apower".
Most of my handlers rely on the fact that a topic carries only a single payload value, not a json object.
Thanks to all for helping!
Josef

Date Sujet#  Auteur
8 May 24 * Which newsgroup for json parsing?29Josef Möllers
8 May 24 +* Re: Which newsgroup for json parsing?2Anton Shepelev
23 May 24 i`- Re: Which newsgroup for json parsing?1Lawrence D'Oliveiro
8 May 24 +* Re: Which newsgroup for json parsing?2Malcolm McLean
8 May 24 i`- Re: Which newsgroup for json parsing?1Anton Shepelev
9 May 24 +- Re: Which newsgroup for json parsing?1jak
13 May 24 +- Re: Which newsgroup for json parsing?1Josef Möllers
16 May 24 `* Re: Which newsgroup for json parsing?22Mikko
16 May 24  +* Re: Which newsgroup for json parsing?19Josef Möllers
16 May 24  i+* Re: Which newsgroup for json parsing?6bart
17 May 24  ii`* Re: Which newsgroup for json parsing?5Malcolm McLean
17 May 24  ii +* Re: Which newsgroup for json parsing?2bart
25 May 24  ii i`- Re: XML (was Re: Which newsgroup for json parsing?)1Lawrence D'Oliveiro
17 May 24  ii `* Re: Which newsgroup for json parsing?2jak
17 May 24  ii  `- Re: Which newsgroup for json parsing?1Malcolm McLean
18 May 24  i`* Re: Which newsgroup for json parsing?12Mikko
27 May 24  i `* Re: Which newsgroup for json parsing?11Josef Möllers
27 May 24  i  `* Re: Which newsgroup for json parsing?10Vir Campestris
28 May 24  i   `* Re: Which newsgroup for json parsing?9Josef Möllers
28 May 24  i    +* Re: Which newsgroup for json parsing?7Michael S
28 May 24  i    i+- Re: Which newsgroup for json parsing?1Josef Möllers
29 May 24  i    i+* Re: Which newsgroup for json parsing?3Malcolm McLean
29 May 24  i    ii`* Re: Which newsgroup for json parsing?2David Brown
30 May 24  i    ii `- Re: Which newsgroup for json parsing?1Malcolm McLean
31 May 24  i    i+- Re: Which newsgroup for json parsing?1Tim Rentsch
5 Aug 24  i    i`- Re: Which newsgroup for json parsing?1Lawrence D'Oliveiro
30 May 24  i    `- Re: Which newsgroup for json parsing?1Vir Campestris
23 May 24  `* Re: Which newsgroup for json parsing?2Lawrence D'Oliveiro
23 May 24   `- Re: Which newsgroup for json parsing?1Malcolm McLean

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal