Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

c - Display the values of a Json file

I have many objects saved in a file as the following form:

{
 "name":"name1",
 "city":"city1",
 "phone":"125698745663" 
},

I have a file that content objects as these last, So I would write a function that take as parameter a file, then give us as an output a list of strings, every string is an object of the file, for example name1 city1 125698745663. Actually I found that a little hard and I have been searching many days for that, I don't want to use libraries already exist cause I have read many documentation but they was difficult to understand for me. I tried to write some functions. That's my essay:

#include<stdlib.h>
/*               
{
     "name":"name1",
     "city":"city1",
     "phone":"125698745663" 
}
/* Boxes of my list */
typedef_struct box
{
   char* current_string;
   box* next;
}box;   
/* Define the structure of the list that I'll return after*/
typedef_struct list
{
   box* beginning;
}list;
/*The function that return the list of the objects that existed in our files*/
**char lire (FILE* my_file) //It is possibly to put the path of the file as an argument
{
      char* nameFile;
      printf("enter the name of the file");
      scanf("%s", &nameFile);
      my_file = fopen( "nameFile","r");
      if (my_file != NULL)
      {
        box* temp;
        int i=0;
        list* my_list;
        int first_time = 0;
        /* browse file elements */
        do
        {
            char c = fgetc(my_file); 
           /* I put the following condition to get the value without its key*/
            if(c == ":")  
            {
                while(c!=",")
                {
                 temp->current_string[i] = c; 
                 i++;
                 // printf("%c",c);  
                 c=fgetc(my_file);
                }        
             }
            /*I put this condition to pass to the following element of the list and fill it*/
            if(c == "}")
            {
                first_time++; 
                /*This condition is for getting the begining of the list because it is what distinguishes the list*/
                if(first_time==1) 
                      my_list->beginning->current_string = temp->current_string;
                temp = temp->next;
            } 
         } while (c != EOF); 
        fclose(my_file);
      }
return my_list;
}```


 
question from:https://stackoverflow.com/questions/65894651/display-the-values-of-a-json-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, I believe that C, especially without JSON library like cjson is not the appropriate tool to do that.

You might have a look to a list of command-line tools related to JSON: https://ilya-sher.org/2018/04/10/list-of-json-tools-for-command-line/

I think the best is to download a command-line tool (pre-compiled if possible) and extract the information you need.

An example how to use jq: Get outputs from jq on a single line

You can download jq from here: https://stedolan.github.io/jq/download/

EDIT: give an example

After jq download, you can use it from the command line cmd.exe:

jq-win64.exe
jq - commandline JSON processor [version 1.6]

Usage:  jq-win64.exe [options] <jq filter> [file...]
        jq-win64.exe [options] --args <jq filter> [strings...]
        jq-win64.exe [options] --jsonargs <jq filter> [JSON_TEXTS...]
...

test.json

[{
 "name":"name1",
 "city":"city1",
 "phone":"125698745663" 
},
{
 "name":"name2",
 "city":"city2",
 "phone":"125698745663" 
}
]

jq command line:

> jq-win64.exe ".[]|.name+","+.city+","+.phone" test.json
"name1,city1,125698745663"
"name2,city2,125698745663"

.[] basically means that one wants to iterate over a JSON array. |.name basically means that one wants to extract name, add a , and .city and so on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...