JsonParser in C with json-c Library | json_object_object_get(…) not declared












0















I am new here and try to implement a Json Parser.
I find a Tutorial (https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/) for a Json Parser Programm.



So i have a Json-File Input like this one:



{ "DefaultTest": "SS",
"Send": {
"ip_hl": 4,
"ip_v": 4,
....
}
}


I use the Code in the Tutorial (https://linuxprograms.wordpress.com/2010/08/19/json_parser_json-c/) and try to understand this one. But i get an Error, that the Method json_object_object_get_get(jobj, key) is not declared.



So find a Wibesite (https://json-c.github.io/json-c/json-c-0.10/doc/html/deprecated.html) with Deprecated List in there for the json-lib. In this there is stay that i should use json_object_object_get_ex.



If I use the method i get this warning: assignment makes pointer from integer without a cast [-Wint-conversion] jobj = json_object_object_get_ex(jobj, key, NULL);



What is this mean? How can I fix it?
My Code:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json/json.h>

void json_parse(json_object * jobj) {
enum json_type type;

json_object_object_foreach(jobj, key, val)
{
printf("type: %dn", type);
type = json_object_get_type(val);

switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
// print_json_value(val);
break;

case json_type_object:
printf("json_type_objectn");

// jobj = json_object_object_get(jobj, key);
jobj = json_object_object_get_ex(jobj, key, NULL);
json_parse(jobj);
break;

case json_type_array:
// printf("json_type_arrayn");
// json_parse_array(jobj,key);
break;

}
}
}

int main() {
char *path = "../Paketspezifikationen/Default-File/DefaultConfig2.json";
FILE *fp;
char *data = NULL;

if ((fp = fopen(path, "rb")) == NULL) {
} else if (fseek(fp, 0, SEEK_END) != 0) {
fclose(fp);
} else {
long size = ftell(fp);
if (size > 0 && (data = (char *) malloc(size + 1)) != NULL) {
fseek(fp, 0, SEEK_SET);
if (fread(data, 1, size, fp) != (size_t) size) {
free(data);
data = NULL;
} else {
data[size] = '';
}
}
fclose(fp);
}
printf("JSON string: %sn", data); // return data

json_object * jobj = json_tokener_parse(data);

json_parse(jobj);
}


In Main I read first the Json File and store it in a String and try to get all my keys and values from there. So i use the json parser.



Hope that you can help me.










share|improve this question























  • Neither the tutorial, nor your code contains json_object_object_get_get. Please copy&paste the exact message. You do not show us a definition for key.

    – Gerhardh
    Nov 19 '18 at 14:49


















0















I am new here and try to implement a Json Parser.
I find a Tutorial (https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/) for a Json Parser Programm.



So i have a Json-File Input like this one:



{ "DefaultTest": "SS",
"Send": {
"ip_hl": 4,
"ip_v": 4,
....
}
}


I use the Code in the Tutorial (https://linuxprograms.wordpress.com/2010/08/19/json_parser_json-c/) and try to understand this one. But i get an Error, that the Method json_object_object_get_get(jobj, key) is not declared.



So find a Wibesite (https://json-c.github.io/json-c/json-c-0.10/doc/html/deprecated.html) with Deprecated List in there for the json-lib. In this there is stay that i should use json_object_object_get_ex.



If I use the method i get this warning: assignment makes pointer from integer without a cast [-Wint-conversion] jobj = json_object_object_get_ex(jobj, key, NULL);



What is this mean? How can I fix it?
My Code:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json/json.h>

void json_parse(json_object * jobj) {
enum json_type type;

json_object_object_foreach(jobj, key, val)
{
printf("type: %dn", type);
type = json_object_get_type(val);

switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
// print_json_value(val);
break;

case json_type_object:
printf("json_type_objectn");

// jobj = json_object_object_get(jobj, key);
jobj = json_object_object_get_ex(jobj, key, NULL);
json_parse(jobj);
break;

case json_type_array:
// printf("json_type_arrayn");
// json_parse_array(jobj,key);
break;

}
}
}

int main() {
char *path = "../Paketspezifikationen/Default-File/DefaultConfig2.json";
FILE *fp;
char *data = NULL;

if ((fp = fopen(path, "rb")) == NULL) {
} else if (fseek(fp, 0, SEEK_END) != 0) {
fclose(fp);
} else {
long size = ftell(fp);
if (size > 0 && (data = (char *) malloc(size + 1)) != NULL) {
fseek(fp, 0, SEEK_SET);
if (fread(data, 1, size, fp) != (size_t) size) {
free(data);
data = NULL;
} else {
data[size] = '';
}
}
fclose(fp);
}
printf("JSON string: %sn", data); // return data

json_object * jobj = json_tokener_parse(data);

json_parse(jobj);
}


In Main I read first the Json File and store it in a String and try to get all my keys and values from there. So i use the json parser.



Hope that you can help me.










share|improve this question























  • Neither the tutorial, nor your code contains json_object_object_get_get. Please copy&paste the exact message. You do not show us a definition for key.

    – Gerhardh
    Nov 19 '18 at 14:49
















0












0








0








I am new here and try to implement a Json Parser.
I find a Tutorial (https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/) for a Json Parser Programm.



So i have a Json-File Input like this one:



{ "DefaultTest": "SS",
"Send": {
"ip_hl": 4,
"ip_v": 4,
....
}
}


I use the Code in the Tutorial (https://linuxprograms.wordpress.com/2010/08/19/json_parser_json-c/) and try to understand this one. But i get an Error, that the Method json_object_object_get_get(jobj, key) is not declared.



So find a Wibesite (https://json-c.github.io/json-c/json-c-0.10/doc/html/deprecated.html) with Deprecated List in there for the json-lib. In this there is stay that i should use json_object_object_get_ex.



If I use the method i get this warning: assignment makes pointer from integer without a cast [-Wint-conversion] jobj = json_object_object_get_ex(jobj, key, NULL);



What is this mean? How can I fix it?
My Code:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json/json.h>

void json_parse(json_object * jobj) {
enum json_type type;

json_object_object_foreach(jobj, key, val)
{
printf("type: %dn", type);
type = json_object_get_type(val);

switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
// print_json_value(val);
break;

case json_type_object:
printf("json_type_objectn");

// jobj = json_object_object_get(jobj, key);
jobj = json_object_object_get_ex(jobj, key, NULL);
json_parse(jobj);
break;

case json_type_array:
// printf("json_type_arrayn");
// json_parse_array(jobj,key);
break;

}
}
}

int main() {
char *path = "../Paketspezifikationen/Default-File/DefaultConfig2.json";
FILE *fp;
char *data = NULL;

if ((fp = fopen(path, "rb")) == NULL) {
} else if (fseek(fp, 0, SEEK_END) != 0) {
fclose(fp);
} else {
long size = ftell(fp);
if (size > 0 && (data = (char *) malloc(size + 1)) != NULL) {
fseek(fp, 0, SEEK_SET);
if (fread(data, 1, size, fp) != (size_t) size) {
free(data);
data = NULL;
} else {
data[size] = '';
}
}
fclose(fp);
}
printf("JSON string: %sn", data); // return data

json_object * jobj = json_tokener_parse(data);

json_parse(jobj);
}


In Main I read first the Json File and store it in a String and try to get all my keys and values from there. So i use the json parser.



Hope that you can help me.










share|improve this question














I am new here and try to implement a Json Parser.
I find a Tutorial (https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/) for a Json Parser Programm.



So i have a Json-File Input like this one:



{ "DefaultTest": "SS",
"Send": {
"ip_hl": 4,
"ip_v": 4,
....
}
}


I use the Code in the Tutorial (https://linuxprograms.wordpress.com/2010/08/19/json_parser_json-c/) and try to understand this one. But i get an Error, that the Method json_object_object_get_get(jobj, key) is not declared.



So find a Wibesite (https://json-c.github.io/json-c/json-c-0.10/doc/html/deprecated.html) with Deprecated List in there for the json-lib. In this there is stay that i should use json_object_object_get_ex.



If I use the method i get this warning: assignment makes pointer from integer without a cast [-Wint-conversion] jobj = json_object_object_get_ex(jobj, key, NULL);



What is this mean? How can I fix it?
My Code:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json/json.h>

void json_parse(json_object * jobj) {
enum json_type type;

json_object_object_foreach(jobj, key, val)
{
printf("type: %dn", type);
type = json_object_get_type(val);

switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
// print_json_value(val);
break;

case json_type_object:
printf("json_type_objectn");

// jobj = json_object_object_get(jobj, key);
jobj = json_object_object_get_ex(jobj, key, NULL);
json_parse(jobj);
break;

case json_type_array:
// printf("json_type_arrayn");
// json_parse_array(jobj,key);
break;

}
}
}

int main() {
char *path = "../Paketspezifikationen/Default-File/DefaultConfig2.json";
FILE *fp;
char *data = NULL;

if ((fp = fopen(path, "rb")) == NULL) {
} else if (fseek(fp, 0, SEEK_END) != 0) {
fclose(fp);
} else {
long size = ftell(fp);
if (size > 0 && (data = (char *) malloc(size + 1)) != NULL) {
fseek(fp, 0, SEEK_SET);
if (fread(data, 1, size, fp) != (size_t) size) {
free(data);
data = NULL;
} else {
data[size] = '';
}
}
fclose(fp);
}
printf("JSON string: %sn", data); // return data

json_object * jobj = json_tokener_parse(data);

json_parse(jobj);
}


In Main I read first the Json File and store it in a String and try to get all my keys and values from there. So i use the json parser.



Hope that you can help me.







c json json-lib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 12:36









SebastianSebastian

14




14













  • Neither the tutorial, nor your code contains json_object_object_get_get. Please copy&paste the exact message. You do not show us a definition for key.

    – Gerhardh
    Nov 19 '18 at 14:49





















  • Neither the tutorial, nor your code contains json_object_object_get_get. Please copy&paste the exact message. You do not show us a definition for key.

    – Gerhardh
    Nov 19 '18 at 14:49



















Neither the tutorial, nor your code contains json_object_object_get_get. Please copy&paste the exact message. You do not show us a definition for key.

– Gerhardh
Nov 19 '18 at 14:49







Neither the tutorial, nor your code contains json_object_object_get_get. Please copy&paste the exact message. You do not show us a definition for key.

– Gerhardh
Nov 19 '18 at 14:49














1 Answer
1






active

oldest

votes


















0














The json_object_object_get_ex() function returns a json_bool (which is actually an int) indicating whether the given key is found in the parent JSON object, so you can't assign the function return value to jobj, which is a pointer: that's where the warning comes from.



Since in your code you already have a pointer (in the val variable) to the JSON object corresponding to a given key, you can achieve what you want more simply by avoiding the call to json_object_object_get_ex() and calling json_parse(val).






share|improve this answer
























  • thank it helps ^^. I try to fix it on my own first. So i have use the 'json_object_object_get_ex(jobj, key, &tmp);' and parse it in a jobj ('jobj = json_tokener_parse(json_object_to_json_string(tmp));') after that i use my 'json_parse(jobj) to parse the obj. I could start the Programm and it hat the impression that it works. Properly, after i test the Programm other problems came. With your solution I was able to fix these problems thank you.

    – Sebastian
    Nov 20 '18 at 13:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374815%2fjsonparser-in-c-with-json-c-library-json-object-object-get-not-declared%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The json_object_object_get_ex() function returns a json_bool (which is actually an int) indicating whether the given key is found in the parent JSON object, so you can't assign the function return value to jobj, which is a pointer: that's where the warning comes from.



Since in your code you already have a pointer (in the val variable) to the JSON object corresponding to a given key, you can achieve what you want more simply by avoiding the call to json_object_object_get_ex() and calling json_parse(val).






share|improve this answer
























  • thank it helps ^^. I try to fix it on my own first. So i have use the 'json_object_object_get_ex(jobj, key, &tmp);' and parse it in a jobj ('jobj = json_tokener_parse(json_object_to_json_string(tmp));') after that i use my 'json_parse(jobj) to parse the obj. I could start the Programm and it hat the impression that it works. Properly, after i test the Programm other problems came. With your solution I was able to fix these problems thank you.

    – Sebastian
    Nov 20 '18 at 13:29
















0














The json_object_object_get_ex() function returns a json_bool (which is actually an int) indicating whether the given key is found in the parent JSON object, so you can't assign the function return value to jobj, which is a pointer: that's where the warning comes from.



Since in your code you already have a pointer (in the val variable) to the JSON object corresponding to a given key, you can achieve what you want more simply by avoiding the call to json_object_object_get_ex() and calling json_parse(val).






share|improve this answer
























  • thank it helps ^^. I try to fix it on my own first. So i have use the 'json_object_object_get_ex(jobj, key, &tmp);' and parse it in a jobj ('jobj = json_tokener_parse(json_object_to_json_string(tmp));') after that i use my 'json_parse(jobj) to parse the obj. I could start the Programm and it hat the impression that it works. Properly, after i test the Programm other problems came. With your solution I was able to fix these problems thank you.

    – Sebastian
    Nov 20 '18 at 13:29














0












0








0







The json_object_object_get_ex() function returns a json_bool (which is actually an int) indicating whether the given key is found in the parent JSON object, so you can't assign the function return value to jobj, which is a pointer: that's where the warning comes from.



Since in your code you already have a pointer (in the val variable) to the JSON object corresponding to a given key, you can achieve what you want more simply by avoiding the call to json_object_object_get_ex() and calling json_parse(val).






share|improve this answer













The json_object_object_get_ex() function returns a json_bool (which is actually an int) indicating whether the given key is found in the parent JSON object, so you can't assign the function return value to jobj, which is a pointer: that's where the warning comes from.



Since in your code you already have a pointer (in the val variable) to the JSON object corresponding to a given key, you can achieve what you want more simply by avoiding the call to json_object_object_get_ex() and calling json_parse(val).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 17:07









Francesco LavraFrancesco Lavra

21317




21317













  • thank it helps ^^. I try to fix it on my own first. So i have use the 'json_object_object_get_ex(jobj, key, &tmp);' and parse it in a jobj ('jobj = json_tokener_parse(json_object_to_json_string(tmp));') after that i use my 'json_parse(jobj) to parse the obj. I could start the Programm and it hat the impression that it works. Properly, after i test the Programm other problems came. With your solution I was able to fix these problems thank you.

    – Sebastian
    Nov 20 '18 at 13:29



















  • thank it helps ^^. I try to fix it on my own first. So i have use the 'json_object_object_get_ex(jobj, key, &tmp);' and parse it in a jobj ('jobj = json_tokener_parse(json_object_to_json_string(tmp));') after that i use my 'json_parse(jobj) to parse the obj. I could start the Programm and it hat the impression that it works. Properly, after i test the Programm other problems came. With your solution I was able to fix these problems thank you.

    – Sebastian
    Nov 20 '18 at 13:29

















thank it helps ^^. I try to fix it on my own first. So i have use the 'json_object_object_get_ex(jobj, key, &tmp);' and parse it in a jobj ('jobj = json_tokener_parse(json_object_to_json_string(tmp));') after that i use my 'json_parse(jobj) to parse the obj. I could start the Programm and it hat the impression that it works. Properly, after i test the Programm other problems came. With your solution I was able to fix these problems thank you.

– Sebastian
Nov 20 '18 at 13:29





thank it helps ^^. I try to fix it on my own first. So i have use the 'json_object_object_get_ex(jobj, key, &tmp);' and parse it in a jobj ('jobj = json_tokener_parse(json_object_to_json_string(tmp));') after that i use my 'json_parse(jobj) to parse the obj. I could start the Programm and it hat the impression that it works. Properly, after i test the Programm other problems came. With your solution I was able to fix these problems thank you.

– Sebastian
Nov 20 '18 at 13:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374815%2fjsonparser-in-c-with-json-c-library-json-object-object-get-not-declared%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?