LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2021, 01:46 PM   #1
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,761
Blog Entries: 23

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
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
 
Old 07-29-2021, 02:59 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 24,129

Rep: Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110Reputation: 7110
Quote:
Originally Posted by ButterflyMelissa View Post
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?
Seems to be two different things here; learning C, and wanting to parse JSON. Obviously, if you don't know C, programming in that language is going to be difficult. I'd suggest starting with either the "jq" Linux utility, which specifically parses JSON, or using either Perl or Python. Either of those languages have less of a learning curve.

Also, saying "read and write" is a bit nebulous...depends on the use case and your data needs.
 
3 members found this post helpful.
Old 07-29-2021, 03:14 PM   #3
lovemeslk
Member
 
Registered: Feb 2020
Location: none of your business
Distribution: Slackware
Posts: 262

Rep: Reputation: Disabled
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

void exportFlashVars()
{
	if(	getenv( "FS_FLASH_PLUGIN" ) && getenv( "FS_FLASH_VERSION" ) ) // User already set those enviroment variables in their shell or in the launcher program
		return;

	char const* pathChecks[] = {
		"/usr/lib64/chromium-browser/PepperFlash/", // Gentoo
		"/usr/lib32/chromium-browser/PepperFlash/", // Gentoo
		"/opt/google/chrome/PepperFlash/", // Mint 17.3 / XBuntu 14.04, probably works with all Ubuntu flavors of that version.
		"/usr/lib/chromium/PepperFlash/", // Slackware
		"/usr/lib64/chromium/PepperFlash/", // Slackware
		"/opt/secondlife-install/PepperFlash/", // In case someone likes to extract pepperflash from a chrome installer on their own, give them a choice with a predef. directory
		NULL
	};

	std::string strExpectedArch = "ia32";
#if ADDRESS_SIZE == 64
	strExpectedArch = "x64";
#endif
	
	for( int i = 0; pathChecks[i]; ++i )
	{
		std::string strPath = pathChecks[i];
		std::string strManifest =  strPath + "manifest.json";
		std::string strPlugin = strPath + "libpepflashplayer.so";
		if( !LLFile::isfile( strManifest ) )
			continue;
		if( !LLFile::isfile( strPlugin ) )
			continue;

		std::ifstream file;
		file.open( strManifest, std::ios::in );
		if( !file.is_open() )
		{
			LL_WARNS( "Flash" ) << "Cannot open " << strManifest << LL_ENDL;
			continue;
		}
		
		Json::Value root;
		Json::Reader reader;
		if (!reader.parse(file,root))
			continue;

		std::string strArch; // ia32 or x64
		std::string strVersion;

		if( root.isMember( "version" ) )
			strVersion = root[ "version" ].asString();
		if( root.isMember( "x-ppapi-arch" ) )
			strArch = root[ "x-ppapi-arch" ].asString();
			
		if( strExpectedArch != strArch )
		{
			LL_WARNS( "Flash" ) << "Arch mismatch expecting: " << strExpectedArch << " got: " << strArch << LL_ENDL;
			continue;
		}

		LL_INFOS( "Flash" ) << "Arch: " << strArch << " version: " << strVersion << " plugin " << strPlugin << LL_ENDL;
	
		setenv( "FS_FLASH_PLUGIN", strPlugin.c_str(), 0 );
		setenv( "FS_FLASH_VERSION", strVersion.c_str(), 0 );
		break;
	}
}
This is an old snippet of how we used to get flash player to work on CEF3 embedded into the linux secondlife viewer.
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:
We parsed the manifest.json got the version
because the call to the browser is pepperflash-version then we set the paths libpepflashplayer.so
flashplayer is in the past. Hope this helps.

Last edited by lovemeslk; 07-30-2021 at 12:44 PM.
 
Old 07-29-2021, 08:51 PM   #4
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,338
Blog Entries: 29

Rep: Reputation: 163Reputation: 163
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.
 
Old 07-30-2021, 12:38 PM   #5
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,761

Original Poster
Blog Entries: 23

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
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
 
Old 07-31-2021, 08:03 AM   #6
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 691

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
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
 
2 members found this post helpful.
Old 08-02-2021, 09:31 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
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 ...")

Last edited by sundialsvcs; 08-02-2021 at 09:57 AM.
 
1 members found this post helpful.
Old 08-03-2021, 04:29 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,612

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
Quote:
Originally Posted by sundialsvcs View Post
I think that you'd find it considerably easier in quite a number of ways to use C++ here.
IMO, learning C++ instead of C will be much harder, not easier.
 
1 members found this post helpful.
Old 08-04-2021, 02:56 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,550

Rep: Reputation: Disabled
Quote:
Originally Posted by ButterflyMelissa View Post
I have some things to meditate over
Master Foo and the Ten Thousand Lines
 
2 members found this post helpful.
Old 08-05-2021, 12:41 PM   #10
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,761

Original Poster
Blog Entries: 23

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
Smile

Quote:
Originally Posted by shruggy View Post
Thank you...I stand enlightened...
 
Old 08-07-2021, 05:37 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
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.
 
1 members found this post helpful.
Old 08-10-2021, 05:42 AM   #12
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,047

Rep: Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074Reputation: 4074
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.
 
Old 08-25-2021, 04:24 PM   #13
mark707
LQ Newbie
 
Registered: Jun 2010
Location: Egypt
Distribution: Arch
Posts: 2

Rep: Reputation: 1
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PERL : anyone have a suggestion for a (pdf) book about JSON parsing? ////// General 6 02-19-2021 02:40 AM
Retrieve the content of a json file using shell script .Without jq /json Historia Linux - Newbie 12 03-25-2020 09:50 AM
LXer: JSON Lines: record-style JSON LXer Syndicated Linux News 0 01-28-2020 08:03 PM
PHP json woes with Owncloud after upgrade to Debian Jessie browny_amiga Linux - Software 2 04-03-2016 07:51 PM
What is Json and jQuery and how are they related to Javascript? Romanus81 Programming 1 08-13-2009 09:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration