linux json-c测试与使用

本文介绍了如何编译和使用json-c库来读取和写入JSON文件。首先,通过配置、编译和安装json-c库。然后,展示了读取名为bios.json的文件内容并获取特定属性的示例代码。最后,提供了写入新内容到bios_future_settings.json文件的代码片段。
摘要由CSDN通过智能技术生成

1.资源

json-c库的资源:

2.编译

$ ./configure --prefix /home/wityuan/Desktop/opensourcecode/json-c-0.13/workspace/
$ make
$ make install

3.使用测试

3.1 读取文件内容

1.json文件,命名为bios.json

{
	"@odata.context": "/redfish/v1/$metadata#Systems/1/Bios",                    
	"@odata.id": "/redfish/v1/Systems/1/Bios",                    
	"@odata.type": "#Bios.v1_0_6.Bios",                    
	"Id": "Bios",                    
	"Name": "BIOS Configuration Current Settings",                    
	"Description": "BIOS Configuration Current Settings",                    
	"AttributeRegistry": "BiosAttributeRegistry0ACMS.1.10.0",
    "Attributes": {
        "QuietBoot": "Enable",
        "TPMSecurityDeviceSupport": "Enable",
        "Pendingoperation": "None",
        "TCMSecurityDeviceSupport": "Enable",
        "TCMState": "Enabled",
        "SHA-1PCRBank": "Disabled",
        "SHA256PCRBank": "Enabled",
        "SM3_256PCRBank": "Disabled",
        "TPM20Pendingoperation": "None",
        "PlatformHierarchy": "Enabled",
        "StorageHierarchy": "Enabled",
        "EndorsementHierarchy": "Enabled",
        "TPM2.0UEFISpecVersion": "TCG_2",
        "PhysicalPresenceSpecVersion": "1.3",
        "BootRetry": "Enable",
        "PxeOnly": "Enable",
        "Bootmodeselect": "UEFI",
        "UEFIBootTypeOrder1": "Hard Disk:CentOS (Micron_5400_MTFDDAV960TG)",
        "UEFIBootTypeOrder2": "NVME",
        "UEFIBootTypeOrder3": "Network",
        "UEFIBootTypeOrder4": "CD/DVD",
        "UEFIBootTypeOrder5": "USB Device",
        "UEFIBootTypeOrder6": "Other Device:UEFI: Built-in EFI Shell",
        "FBO301": "Hard Disk",
        "LegacyBootTypeOrder1": "Hard Disk",
        "LegacyBootTypeOrder2": "NVME",
        "LegacyBootTypeOrder3": "Network",
        "LegacyBootTypeOrder4": "CD/DVD",
        "LegacyBootTypeOrder5": "USB Device",
        "LegacyBootTypeOrder6": "Other Device",
        "FRB2Timer": "Enable",
        "FRB2timeout": 15,
        "FRB2Policy": "Do Nothing",
        "OSWtdTimer": "Disable",
        "OSWtdTimeout": 10,
        "OSWtdPolicy": "Reset",
        "PowerControlPolicy": "Unspecified",
        "NCSISupport": "Enable",
        "PowerMode": "Custom",
        "Numa": "Enable",
        "LinkFrequencySelect": "Auto",
        "UPILinkDisable": "Enabled All Links",
        "ioutslope": "Auto",
        "PowerPerformanceTuning": "OS Controls EPB",
        "ENERGYPERFBIASCFGMode": "Balanced Performance",
        "DynamicLoadlineSwitch": "Enable",
        "WorkloadConifg": "Balanced",
        "KTILinkL0p": "Disable",
        "KTILinkL1": "Disable",
        "EnableMonitorMWAIT": "Enable",
        "CStates": "Enable",
        "C1EnhancedMode": "Enable",
        "CPUC6Report": "Enable",
        "PackageCState": "Auto",
        "WakeOnLanSupport": "Disable",
        "SystemErrors": "Enable",
        "SystemMemoryPoison": "Enable",
        "EMCALogging": "Enable",
        "EMCACMCI-SMIMorphing": "EMCA gen 2 CSMI",
        "EMCAMCE-SMIEnable": "EMCA gen 2 - MSMI",
        "WHEAErrorInjSupport": "Disable",
        "SetPowerOnPassword": null,
        "SetAdministratorPassword": null,
        "NetworkStack": "Enable",
        "IPv4PXESupport": "Enabled",
        "IPv4HTTPSupport": "Disable",
        "IPv6PXESupport": "Disabled",
        "IPv6HTTPSupport": "Disable",
        "PXEwaittime": 0,
        "Mediadetectcount": 1,
        "MemorySpeed": "Automatic",
        "MemorySpeedMode": "Performance",
        "SNCx": "Disable",
        "UMABasedClustering": "Hemisphere (2-clusters)",
        "MirrorMode": "Disable",
        "PartialMirrorSize": "8G",
        "Sparingx": "Disable",
        "Leakybuckettimewindow": "Enable",
        "Leakybucketlowbit": 40,
        "Leakybuckethighbit": 41,
        "LeakybucketTimeWindowHour": 0,
        "LeakybucketTimeWindowHourMinute": 0,
        "LeakybucketTimeWindowHourSecond": 1,
        "MultiRankSparing": "Two Rank",
        "SDDCx": "Disable",
        "PCLS": "Disable",
        "ADDDCSparing": "Disable",
        "PPRType": "Soft PPR",
        "PatrolScrub": "Enable",
        "MemoryDataScrambling": "Enable",
        "PagePolicy": "Adaptive",
        "APDx": "Disable",
        "2xRefreshEnable": "Automatic",
        "ThrottlingMode": "CLTT",
        "HyperThreading": "Enabled",
        "DisableBitmap0": 0,
        "DisableBitmap1": 0,
        "ProcessorPerformanceStates": "Enable",
        "IntelSSTPP": "Base",
        "DynamicSSTPP": "Disable",
        "ActivateSSTBF": "Disable",
        "ConfigureSSTBF": "Enable",
        "TurboMode": "Enable",
        "HardwarePStates": "Native Mode",
        "EnergyEfficientTurbo": "Enable",
        "UncoreFrequencyScaling": "Disable",
        "UncoreFrequency": 1300,
        "TrustedExecutionTechnology": "Disable",
        "IntelVirtualizationTechnology": "Enable",
        "LimitCpuPAto46bits": "Enable",
        "HardwarePrefetcher": "Enable",
        "AdjacentCachePrefetcher": "Enable",
        "DCUStreamerPrefetcher": "Enable",
        "DCUIPPrefetcher": "Enable",
        "LLCPrefetch": "Enable",
        "XPTPrefetcher": "Disable",
        "UPIPrefetcher": "Enable",
        "AesEnable": "Enable",
        "TotalMemoryEncryption": "Disable",
        "TotalMemoryEncryptionMultiTenant": "Disable",
        "SGXFactoryReset": "Disable",
        "SWGuardExtensions": "Disable",
        "SGXPackageInfoInBandAccess": "Disable",
        "PRMRRSize": "No valid PRMRR size",
        "SGXQoS": "Enable",
        "SelectOwnerEPOCHinputtype": "Manual User Defined Owner EPOCHs",
        "SoftwareGuardExtensionsEpoch0": 0,
        "SoftwareGuardExtensionsEpoch1": 0,
        "SGXLEPUBKEYHASHxWriteEnable": "Enable",
        "SGXLEPUBKEYHASH0": 0,
        "SGXLEPUBKEYHASH1": 0,
        "SGXLEPUBKEYHASH2": 0,
        "SGXLEPUBKEYHASH3": 0,
        "EnableDisableSGXAutoMPRegistrationAgent": "Disable",
        "ActiveVideo": "Onboard Device",
        "ABOVE4G": "Enable",
        "MaxPayload": "Auto",
        "PcieASPMSupport": "No",
        "MMIOLBase": "Auto",
        "MMIOHBase": "32T",
        "MMIOHGranularity": "64G",
        "VtdSupport": "Enabled",
        "SRIOV": "Enable",
        "ARIForwarding": "Disable",
        "HardDiskWriteCaching": "Enable",
        "IntelVMDSupport": "Disable",
        "SATAController": "Enable",
        "OnboardSATAMode": "AHCI",
        "SATA4": "Enable",
        "SATA5": "Enable",
        "SATA6": "Enable",
        "SATA7": "Enable",
        "SATA8": "Enable",
        "SATA9": "Enable",
        "SATA10": "Enable",
        "SATA11": "Enable",
        "sSATAController": "Enable",
        "OnboardsSATAMode": "AHCI",
        "M.2-0": "Enable",
        "M.2-1": "Enable",
        "SATA0": "Enable",
        "SATA1": "Enable",
        "SATA2": "Enable",
        "SATA3": "Enable",
        "USBMassSupport": "Enable",
        "USB20BMCUSBPort": "Enable",
        "USB20FrontUSB20header": "Enable",
        "USB20FrontUSB30header": "Enable",
        "USB30FrontUSB30header": "Enable",
        "USB20RearPort3": "Enable",
        "USB30RearPort3": "Enable",
        "USB20RearPort4": "Enable",
        "USB30RearPort4": "Enable",
        "USB20RearPort5": "Enable",
        "USB30RearPort5": "Enable",
        "OCPNIC": "Enable",
        "OCPNICLinkSpeed": "Auto",
        "OCPNICOptionROM": "Auto",
        "COM2Redirection": "Enable",
        "COM2BaudRate": "115200",
        "COM2DataBits": "8",
        "COM2Parity": "None",
        "COM2StopBits": "1",
        "COM2TerminalEmulation": "VT100",
        "COM2FlowControl": "Disable"
    }
}

可以使用如下代码测试功能:

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

#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
#include "json_util.h"

int main()
{
    const char *filename = "./bios.json";
    
    json_object *attribute = NULL;
    json_object *tmpjson = NULL;
    json_object *powermode = NULL;

    printf(" hello my test...\n");

    json_object *jso = json_object_from_file(filename);
    if (jso == NULL)
    {
        return -1;
    }
    else
    {
        json_object_object_get_ex(jso, "Attributes", &attribute);
        if(attribute == NULL)
        {
            printf("attribute is null \n");
            return -1;
        }
        printf(" attribute is : %s \n" , json_object_get_string(attribute));

        json_object_object_get_ex(attribute, "PowerMode", &powermode);
        if(powermode == NULL)
        {
            printf("powermode is null \n");
            return -1;
        }
        printf(" powermode is : %s \n" , json_object_get_string(powermode));
    }

    return 0;
}

编译方法:

$ gcc mytest1.c -o mytest1 -I /home/wityuan/Desktop/opensourcecode/json-c-0.13/workspace/include/json-c/  -L/home/wityuan/Desktop/opensourcecode/json-c-0.13/workspace/lib/ -ljson-c

3.2 写入文件内容

例如,命令一个json文件:bios_future_settings.json:

{"AttributeRegistry":"BiosAttributeRegistry0ACMS.1.10.0","Attributes":{"ProcessorPerformanceStates":"Disable"}}

使用如下代码测试:

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

#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
#include "json_util.h"


int read_json_file_test()
{
    //const char *filename = "./bios.json";
    const char *filename = "./bios_current_settings.json";
    
    json_object *attribute = NULL;
    json_object *tmpjson = NULL;
    json_object *powermode = NULL;

    printf(" hello my test...\n");

    json_object *jso = json_object_from_file(filename);
    if (jso == NULL)
    {
        return -1;
    }
    else
    {
        json_object_object_get_ex(jso, "Attributes", &attribute);
        if(attribute == NULL)
        {
            printf("attribute is null \n");
            return -1;
        }
        printf(" attribute is : %s \n" , json_object_get_string(attribute));

        json_object_object_get_ex(attribute, "PowerMode", &powermode);
        if(powermode == NULL)
        {
            printf("powermode is null \n");
            return -1;
        }
        printf(" powermode is : %s \n" , json_object_get_string(powermode));
    }
}


int write_json_file_test()
{
    //const char *filename = "./bios.json";
    const char *filename = "./bios_future_settings.json";
    
    json_object *attribute = NULL;
    json_object *powermode = NULL;
    json_object *tmpjson = NULL;

    json_object *new_attribute_obj = json_object_new_object();

    printf(" hello my test write json file...\n");

    json_object *jso = json_object_from_file(filename);
    if (jso == NULL)
    {
        return -1;
    }
    else
    {
        //  json_object_object_add(jso, "Attributes", new_attribute_obj);
        //json_object_object_add(new_attribute_obj, "power",  json_object_new_string("enable"));
        json_object_object_del(jso,"Attributes");
        json_object_to_file(filename, jso);
        
        json_object_put(jso);
        
    }    
    return 0;
}

int main()
{
    write_json_file_test();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值