mirror of
https://github.com/rafagafe/tiny-json.git
synced 2025-07-16 16:12:08 +00:00
The example files are renamed.
This commit is contained in:
parent
cd6b0a0749
commit
27ea70d9cc
@ -9,14 +9,14 @@
|
|||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* In this example the JSON library is used to analyze an object that some
|
* In this example the JSON library is used to analyze an object that some
|
||||||
* properties are expected.
|
* properties are expected.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -26,7 +26,7 @@
|
|||||||
#include "tiny-json.h"
|
#include "tiny-json.h"
|
||||||
|
|
||||||
/* Parser a json string. */
|
/* Parser a json string. */
|
||||||
int main( void ) {
|
int main( void ) {
|
||||||
char const* msg = "{\n"
|
char const* msg = "{\n"
|
||||||
"\t\"firstName\": \"Bidhan\",\n"
|
"\t\"firstName\": \"Bidhan\",\n"
|
||||||
"\t\"lastName\": \"Chatterjee\",\n"
|
"\t\"lastName\": \"Chatterjee\",\n"
|
||||||
@ -41,7 +41,7 @@ int main( void ) {
|
|||||||
"\t\t{ \"type\": \"personal\", \"number\": \"09832209761\" },\n"
|
"\t\t{ \"type\": \"personal\", \"number\": \"09832209761\" },\n"
|
||||||
"\t\t{ \"type\": \"fax\", \"number\": \"91-342-2567692\" }\n"
|
"\t\t{ \"type\": \"fax\", \"number\": \"91-342-2567692\" }\n"
|
||||||
"\t]\n"
|
"\t]\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
char aux[512];
|
char aux[512];
|
||||||
strcpy( aux, msg );
|
strcpy( aux, msg );
|
||||||
puts( aux );
|
puts( aux );
|
||||||
@ -49,7 +49,7 @@ int main( void ) {
|
|||||||
json_t const* json = json_create( aux, mem, sizeof mem / sizeof *mem );
|
json_t const* json = json_create( aux, mem, sizeof mem / sizeof *mem );
|
||||||
if ( !json ) {
|
if ( !json ) {
|
||||||
puts("Error json create.");
|
puts("Error json create.");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t const* firstName = json_getProperty( json, "firstName" );
|
json_t const* firstName = json_getProperty( json, "firstName" );
|
||||||
@ -59,29 +59,29 @@ int main( void ) {
|
|||||||
}
|
}
|
||||||
char const* firstNameVal = json_getValue( firstName );
|
char const* firstNameVal = json_getValue( firstName );
|
||||||
if ( firstNameVal ) printf( "Fist Name: %s.\n", firstNameVal );
|
if ( firstNameVal ) printf( "Fist Name: %s.\n", firstNameVal );
|
||||||
|
|
||||||
json_t const* lastName = json_getProperty( json, "lastName" );
|
json_t const* lastName = json_getProperty( json, "lastName" );
|
||||||
if ( !lastName || JSON_TEXT != json_getType( lastName ) ) {
|
if ( !lastName || JSON_TEXT != json_getType( lastName ) ) {
|
||||||
puts("Error, the last name property is not found.");
|
puts("Error, the last name property is not found.");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
char const* lastNameVal = json_getValue( lastName );
|
char const* lastNameVal = json_getValue( lastName );
|
||||||
if ( lastName ) printf( "Last Name: %s.\n", lastNameVal );
|
if ( lastName ) printf( "Last Name: %s.\n", lastNameVal );
|
||||||
|
|
||||||
json_t const* age = json_getProperty( json, "age" );
|
json_t const* age = json_getProperty( json, "age" );
|
||||||
if ( !age || JSON_INTEGER != json_getType( age ) ) {
|
if ( !age || JSON_INTEGER != json_getType( age ) ) {
|
||||||
puts("Error, the age property is not found.");
|
puts("Error, the age property is not found.");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
int const ageVal = (int)json_getInteger( age );
|
int const ageVal = (int)json_getInteger( age );
|
||||||
printf( "Age: %d.\n", ageVal );
|
printf( "Age: %d.\n", ageVal );
|
||||||
|
|
||||||
json_t const* phoneList = json_getProperty( json, "phoneList" );
|
json_t const* phoneList = json_getProperty( json, "phoneList" );
|
||||||
if ( !phoneList || JSON_ARRAY != json_getType( phoneList ) ) {
|
if ( !phoneList || JSON_ARRAY != json_getType( phoneList ) ) {
|
||||||
puts("Error, the phone list property is not found.");
|
puts("Error, the phone list property is not found.");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t const* phone;
|
json_t const* phone;
|
||||||
for( phone = json_getChild( phoneList ); phone != 0; phone = json_getSibling( phone ) ) {
|
for( phone = json_getChild( phoneList ); phone != 0; phone = json_getSibling( phone ) ) {
|
||||||
if ( JSON_OBJ == json_getType( phone ) ) {
|
if ( JSON_OBJ == json_getType( phone ) ) {
|
||||||
@ -90,9 +90,8 @@ int main( void ) {
|
|||||||
char const* numberVal = json_getValue( number );
|
char const* numberVal = json_getValue( number );
|
||||||
if ( numberVal ) printf( "Number: %s.\n", numberVal );
|
if ( numberVal ) printf( "Number: %s.\n", numberVal );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
18
makefile
18
makefile
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
|
|
||||||
build: example1.exe example2.exe
|
build: example-01.exe example-02.exe
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf *.o
|
rm -rf *.o
|
||||||
@ -8,17 +8,17 @@ clean:
|
|||||||
|
|
||||||
all: clean build
|
all: clean build
|
||||||
|
|
||||||
example1.exe: example1.o tiny-json.o
|
example-01.exe: example-01.o tiny-json.o
|
||||||
gcc -o example1.exe example1.o tiny-json.o
|
gcc -o example-01.exe example-01.o tiny-json.o
|
||||||
|
|
||||||
example2.exe: example2.o tiny-json.o
|
example-02.exe: example-02.o tiny-json.o
|
||||||
gcc -o example2.exe example2.o tiny-json.o
|
gcc -o example-02.exe example-02.o tiny-json.o
|
||||||
|
|
||||||
tiny-json.o: tiny-json.c tiny-json.h
|
tiny-json.o: tiny-json.c tiny-json.h
|
||||||
gcc -c tiny-json.c
|
gcc -c tiny-json.c
|
||||||
|
|
||||||
example1.o: example1.c tiny-json.h
|
example-01.o: example-01.c tiny-json.h
|
||||||
gcc -c example1.c
|
gcc -c example-01.c
|
||||||
|
|
||||||
example2.o: example2.c tiny-json.h
|
example-02.o: example-02.c tiny-json.h
|
||||||
gcc -c example2.c
|
gcc -c example-02.c
|
||||||
|
Loading…
Reference in New Issue
Block a user