Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am using frozen.h for json files .. I want to read the book array info from .json file into an array of structs
***********problem 1**********

.. struct book has a nested struct date which I want to access in .json file .. but the library won't allow me .. if you know a solution to my problem with frozen.h OR you'd recommend another library for json files in C that supports the following feature:
* read/write from/into json file and array of structs that has nested structs
*scans a string directly into C variables
* prints C variables directly into an output stream
* modifies an existing JSON string

*********problem 2**********

when storing the book title in array[0].title for example the saved string is Automation only not Automation and Robotics

What I have tried:

#include <stdio.h>
#include "frozen.h"
#include <stdlib.h>
#include <string.h>
typedef struct
{

    int day;
    int month;
    int year;
} dateStruct;

typedef struct
{

    char title[10];
    char author[10];
    char publisher[10];
    char ISBN[10];
    dateStruct date;
    int copies;
    int current;
    int times_borrowed;
} book;
int main()
{   book array[3];

  char *content = json_fread("tmp.json");
//tmp.json exists near main.c 
//and written in json format
  int i, value, len = strlen(content);
  struct json_token t;
  for (i = 0; json_scanf_array_elem(content, len, ".book", i, &t) > 0; i++) {
    json_scanf(t.ptr, t.len, "{title: %s}", &(ar[i].title));
    printf("%s\n\n",array[i].title);

  }
    return 0;
}


==========the json file ============
Quote:
{
"book": [

{
"title": "Automation And Robotics",
"author": "Miltiadis Boboulos",
"publisher": "Academic Studies Press",
"ISBN": "978-87-7681-696-4",
"date" : [
{
"day":14
"month":12
"year":2017
} ],
"copies":5
"current":3



},

{
"title": "engineering",
"author": "Abimbola Windapo",
"publisher": "Allen & Unwin",
"ISBN": "978-87-403-0362-9",
"date" : [
{
"day":14
"month":12
"year":2017
} ],
"copies":7
"current":6

}

]
}
Posted
Updated 12-Jan-18 4:48am
Comments
Richard MacCutchan 21-Dec-17 3:48am    
frozen.h is a C/C++ header file. What actual library are you trying to use, and what does the documentation say?

1 solution

you arent using the library in the correct way. Take a look at the sample code on Github to understand it. The key fuction is json_scanf which can have different parameter like a function pointer to scan subobjects. The rest is some nesting of the code to match your data structure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900