JSONライブラリ サンプルプログラムです。
本サンプルでは、下記を実行することができます。
- JSON文字列を解析し、ファイルに保存する。
- JSON文字列を生成し、ファイルに保存する。
本サンプルに使用するには、次のライブラリをアプリケーションプログラムにリンクしてください。
ヘッダファイル:
lib.h : システム18.0以降ライブラリファイル:
jansson.h
jansson_config.h
jansson_log.h
CodeConversion.h : ver.1.1.1以降
libJansson.a
libCodeConversion.a : ver.1.1.1以降
libSTARTUPOPH5000.a : システム18.0以降
(注意)
ライブラリファイルの追加を行うときは上記の順番に合わせてリンクしてください。
ParseJson()
次のJSON文字列を解析し、ファイルに保存します。
[
{
"名前": "サンプル001",
"itemID": [
"ID001"
],
"score": 0,
"result": null
},
{
"名前": "サンプル002",
"itemID": [
"ID001",
"ID002"
],
"score": 35.5,
"result": false
},
{
"名前": "サンプル003",
"itemID": [
"ID001",
"ID003"
],
"score": 95,
"result": true
}
]
EditJson()
次のJSON文字列を生成し、ファイルに保存します。
{
"object1": {
"名前": "サンプル001",
"itemID": [
"ID001"
],
"score": 15.5,
"result": null
},
"object2": {
"名前": "サンプル002",
"itemID": [
"ID002"
],
"score": 35.5,
"result": false
},
"object3": {
"名前": "サンプル003",
"itemID": [
"ID001",
"ID002"
],
"score": 90,
"result": true
},
"object4": {
"名前": "サンプル004",
"itemID": [
"ID001",
"ID003"
],
"score": 95,
"result": true
}
}
サンプル: main.c
#include <stdio.h>
#include <string.h>
#include "lib.h"
#include "CodeConversion.h"
#include "logapi.h"
#include "jansson.h"
#define JSON_LOG_TAG "[JSON]"
#define JSON_SUCCESS 0
#define JSON_SYSTEM_ERROR (-1)
int DBG_JANSSON_API_ERROR = 1;
int DBG_JANSSON_API_INFO = 1;
int DBG_JANSSON_API_DEBUG = 0;
int DBG_JSON_SAMPLE_ERROR = 1;
int DBG_JSON_SAMPLE_INFO = 1;
int DBG_JSON_SAMPLE_DEBUG = 0;
int jsonOutput(json_t* pRoot, char* pFilePath);
int ParseJson();
int EditJson();
const char* jsonText1 = "["
"{\"名前\": \"サンプル001\", \"itemID\": [\"ID001\"], "
"\"score\": 0, \"result\": null}, "
"{\"名前\": \"サンプル002\", \"itemID\": [\"ID001\", \"ID002\"], "
"\"score\": 35.5, \"result\": false},"
"{\"名前\": \"サンプル003\", \"itemID\": [\"ID001\", \"ID003\"], "
"\"score\": 95, \"result\": true}"
"]";
void main(void)
{
Cursor(AUTOWRAP);
while (1) {
printf("1:Parse JSON\n");
printf("2:Edit JSON\n");
while (1) {
if (kbhit()) {
int key = getchar();
if (key == '1') {
ParseJson();
break;
}
if (key == '2') {
EditJson();
break;
}
}
Idle();
}
}
}
int jsonOutput(json_t* pRoot, char* pFilePath)
{
int result = 0;
char* pJsonText = NULL;
char* pEncodeText = NULL;
char* pSeparateText = NULL;
int errorCode = 0;
size_t outputLength = 0;
pJsonText = json_dumps(pRoot, JSON_ENCODE_ANY | JSON_INDENT(2));
if (!pJsonText) {
LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s jsonOutput. json_dumps error.\n", JSON_LOG_TAG);
return JSON_SYSTEM_ERROR;
}
pEncodeText = CONV_Utf8ToSjisString(pJsonText, strlen(pJsonText),
&errorCode, &outputLength);
if (!pEncodeText) {
LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s jsonOutput. convert error. errorCode: %d.\n",
JSON_LOG_TAG, errorCode);
free(pJsonText);
return JSON_SYSTEM_ERROR;
}
LOG_PRINTF(DBG_JSON_SAMPLE_INFO, "%s JSON Data: \n", JSON_LOG_TAG);
pSeparateText = strtok(pEncodeText, "\n");
LOG_PRINTF(DBG_JSON_SAMPLE_INFO, "%s %s\n", JSON_LOG_TAG, pSeparateText);
while (pSeparateText != NULL) {
pSeparateText = strtok(NULL, "\n");
LOG_PRINTF(DBG_JSON_SAMPLE_INFO, "%s %s\n", JSON_LOG_TAG, pSeparateText);
}
free(pEncodeText);
free(pJsonText);
result = json_dump_file(pRoot, pFilePath, JSON_ENCODE_ANY | JSON_INDENT(2));
if (result == -1) {
LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s json_dump_file error.\n", JSON_LOG_TAG);
return JSON_SYSTEM_ERROR;
}
return JSON_SUCCESS;
}
int ParseJson()
{
json_t* pRoot = NULL;
json_error_t error;
char* pUtf8Text = NULL;
int errorCode = 0;
size_t outputLength = 0;
pUtf8Text = CONV_SjisToUtf8String((char*)jsonText1, strlen(jsonText1),
&errorCode, &outputLength);
if (!pUtf8Text) {
LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s ParseJson. convert error. errorCode: %d.\n",
JSON_LOG_TAG, errorCode);
return JSON_SYSTEM_ERROR;
}
pRoot = json_loads(pUtf8Text, 0, &error);
free(pUtf8Text);
if (!pRoot) {
LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s json_loads. error: on line %d: %s\n",
JSON_LOG_TAG, error.line, error.text);
return JSON_SYSTEM_ERROR;
}
int result = jsonOutput(pRoot, "JSON_S1.TXT");
json_decref(pRoot);
if (result != JSON_SUCCESS) {
return JSON_SYSTEM_ERROR;
}
return JSON_SUCCESS;
/* Output
[
{
"名前": "サンプル001",
"itemID": [
"ID001"
],
"score": 0,
"result": null
},
{
"名前": "サンプル002",
"itemID": [
"ID001",
"ID002"
],
"score": 35.5,
"result": false
},
{
"名前": "サンプル003",
"itemID": [
"ID001",
"ID003"
],
"score": 95,
"result": true
}
]
*/
}
int EditJson()
{
json_t* pRoot = NULL;
int errorCode = 0;
size_t outputLength = 0;
char* pUtf8Text_name = NULL;
char* pUtf8Text_sample1 = NULL;
char* pUtf8Text_sample2 = NULL;
char* pUtf8Text_sample3 = NULL;
char* pUtf8Text_sample4 = NULL;
pUtf8Text_name = CONV_SjisToUtf8String("名前", strlen("名前"),
&errorCode, &outputLength);
pUtf8Text_sample1 = CONV_SjisToUtf8String("サンプル001", strlen("サンプル001"),
&errorCode, &outputLength);
pUtf8Text_sample2 = CONV_SjisToUtf8String("サンプル002", strlen("サンプル002"),
&errorCode, &outputLength);
pUtf8Text_sample3 = CONV_SjisToUtf8String("サンプル003", strlen("サンプル003"),
&errorCode, &outputLength);
pUtf8Text_sample4 = CONV_SjisToUtf8String("サンプル004", strlen("サンプル004"),
&errorCode, &outputLength);
if ((!pUtf8Text_name) || (!pUtf8Text_sample1) || (!pUtf8Text_sample2)
|| (!pUtf8Text_sample3) || (!pUtf8Text_sample4))
{
LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s EditJson. convert error.\n", JSON_LOG_TAG);
goto Exit;
}
pRoot = json_pack("{s:{s:s, s:[s], s:f, s:n}, s:{s:s, s:[s], s:f, s:b}, "
"s:{s:s, s:[s, s], s:i, s:b}, s:{s:s, s:[s, s], s:i, s:b}}",
"object1", pUtf8Text_name, pUtf8Text_sample1,
"itemID", "ID001", "score", 15.5, "result",
"object2", pUtf8Text_name, pUtf8Text_sample2,
"itemID", "ID002", "score", 35.5, "result", false,
"object3", pUtf8Text_name, pUtf8Text_sample3,
"itemID", "ID001", "ID002", "score", 90, "result", true,
"object4", pUtf8Text_name, pUtf8Text_sample4,
"itemID", "ID001", "ID003", "score", 95, "result", true);
if (!pRoot) {
LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s EditJson. json_pack error.\n", JSON_LOG_TAG);
goto Exit;
}
free(pUtf8Text_name);
free(pUtf8Text_sample1);
free(pUtf8Text_sample2);
free(pUtf8Text_sample3);
free(pUtf8Text_sample4);
int result = jsonOutput(pRoot, "JSON_S2.TXT");
json_decref(pRoot);
if (result != JSON_SUCCESS) {
return JSON_SYSTEM_ERROR;
}
return JSON_SUCCESS;
Exit:
if (pUtf8Text_name)
free(pUtf8Text_name);
if (pUtf8Text_sample1)
free(pUtf8Text_sample1);
if (pUtf8Text_sample2)
free(pUtf8Text_sample2);
if (pUtf8Text_sample3)
free(pUtf8Text_sample3);
if (pUtf8Text_sample4)
free(pUtf8Text_sample4);
return JSON_SYSTEM_ERROR;
/* Output
{
"object1": {
"名前": "サンプル001",
"itemID": [
"ID001"
],
"score": 15.5,
"result": null
},
"object2": {
"名前": "サンプル002",
"itemID": [
"ID002"
],
"score": 35.5,
"result": false
},
"object3": {
"名前": "サンプル003",
"itemID": [
"ID001",
"ID002"
],
"score": 90,
"result": true
},
"object4": {
"名前": "サンプル004",
"itemID": [
"ID001",
"ID003"
],
"score": 95,
"result": true
}
}
*/
}
関連事項
最終更新日:2022/01/26