JSON-C and the woes I have...
Hi all :)
In an effort to learn C, I try to parse JSON data and use it in my program. I use JSON-C for that (see github https://github.com/json-c/json-c ) but the tutorial is nearly impossible for me. Mostly due to the fact that I dont know any C as well, of course. Anyone have experience with said lib? Or...can suggest an alternative that can read and write (update settings to persist them) JSON? Thanks Melissa |
Quote:
Also, saying "read and write" is a bit nebulous...depends on the use case and your data needs. |
Have you tried using this tutorial
https://gist.github.com/alan-mushi/19546a0e2c6bd4e059fd one of the projects I work with use Json in c++ Code:
#include "json/reader.h" // To parse manifest.json from pepperflash but for just learning C you may want to look at programs how they implement read parse write. the code was written by NickyD team lead for Firestorm secondlife client. We needed json because we had to parse the pepperflash manifest.json for version and turn that into a variable. Quote:
|
if your goal is to learn C then i suggest not trying to parse any JSON or using most non-standard libraries. most of them have you work with pointers in complex ways. and a typical JSON library assumes reading (or writing) files in whole.
if your goal is to parse JSON then C is a poor choice of language to learn. i suggest Perl or Python. i have a few scripts i have written that work with JSON or compressed JSON, all done in Python. i reserve using C for special cases, now (since learning Python a few years ago), and JSON is not one of them. i just cannot imagine ever doing any JSON in C. if your goal is to acquire career skills, then consider JSON and C to be "either/or". just about any job that needs either will not need the other. |
Thanks all,
The goal is (perhaps) weirder...but...I may, as TBOne seems to hint, break things down in learning one thing at a time. The ultimate goal: C, NCurses and then to persist local data on disk. JSON seemed a logical choice (from my point of view which clearly is incomplete). The aim of JSON was simply to persist some settings locally. XML could be an option too, or flat text... It's not a carreer choice/option at my age anymore. I have 57 candles on my particular cake...not much carreer left ;) As lovemeslk suggested...Python. And...why not, really... I have some things to meditate over :) Thanks and hugs all round Melissa |
The C libraries' fopen, fprintf, and fscanf routines provide an easy way to write and read files. These should be among the first library calls that a C programmer learns.
Ed |
I think that you'd find it considerably easier – in quite a number of ways – to use C++ here.
In particular, C++ gives you a taxonomy of "objects," which are a very-natural way to express the data structures that are inherent in a JSON stream. It also gives you much-more-sophisticated primitive data objects, such as a well-behaved string type. And the efficiency is just as good as "C." It's just a heck of a lot easier to use! :) ("Whenever possible, always let the language do the dirty work ...") |
Quote:
|
Quote:
|
Quote:
|
I suppose it's up to you whether you consider C++ "hard to learn," but I now find it to be much easier. The C++ language makes it very easy for you to use more sophisticated data-types – such as a well-designed string type – as well as a rich library of "containers" and other classes. The implementation is efficient, but the language does more work for you.
|
I guess it depends on how deep you go. C++ language itself is not that much more complex than C, and having the std::string certainly helps, but as soon as you start getting involved with OO, the STL stuff, vectors, iterators and all that fancy jazz things take on a far more complex turn.
Also, if the OP is planning to learn ncurses, then probably best to stick with C which is its native binding. For the OP, rather than jumping straight into parsing more complex/structured formats like json or xml, I'd start with simple key=value format datafiles, which are nice and easy to parse with standard C library functions such as strtok() or even just using old-school fscanf() and fprintf() as EdGr suggested above. Walk before you run... Oh, and listen to Master Foo. ;) |
String handling in C is not trivial, Especially since there is no native string data type in C. I suggest you take your time with C and do your JSON parsing with a scripting language.
|
All times are GMT -5. The time now is 10:50 AM. |