访问json文件中的嵌套结构 [英] Access nested struct in json file

查看:152
本文介绍了访问json文件中的嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 frozen.h 获取json文件..我想将 .json文件中的书籍数组信息读入结构数组

*********** 问题1 **********



.. struct book有一个嵌套的struct日期,我想在.json文件中访问..但是库不会允许我..如果你知道我的解决方案freeze.h的问题或者你建议在C中使用另一个支持以下功能的json文件库:

*读/写json文件和嵌套结构的结构数组

*将字符串直接扫描到C变量中

*将C变量直接打印到输出流中

*修改现有的JSON字符串



********* 问题2 **********



将书名存储在数组[0] .title中时,例如保存的字符串是自动化而不是自动化和机器人



我尝试过:



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;
}





========== json file ============



==========the json file ============

Quote:

{

预订:[



{

title:Automation and Robotics,

作者:Miltiadis Boboulos,

出版者:学术研究出版社,

ISBN:978-87-7681- 696-4,

日期:[

{

day:14

月:12

年:2017

}],

副本:5

当前:3







},



{

title:engineering,

author:Abimbola Windapo,

publisher: Allen& Unwin,

ISBN:978-87-403-0362-9,

日期:[

{

day:14

month:12

年:2017

}],

副本:7

当前:6



}



]

}

{
"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

}

]
}

推荐答案

你没有以正确的方式使用该库。请查看 Github上的示例代码以了解它。关键功能是json_scanf,它可以有不同的参数,如扫描子对象的函数指针。其余的是代码嵌套以匹配您的数据结构。
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.


这篇关于访问json文件中的嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆