❶ 怎么用C语言获取jsON中的数据
用C语言获取JSON中的数据的方法是使用 CJSON。
以下简单介绍用CJSON的思路及实现:
1)创建json,从json中获取数据。
#nclude <stdio.h>
#include "cJSON.h"
char * makeJson()
{
cJSON * pJsonRoot = NULL;
pJsonRoot = cJSON_CreateObject();
if(NULL == pJsonRoot)
{
//error happend here
return NULL;
}
cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");
cJSON_AddNumberToObject(pJsonRoot, "number", 10010);
cJSON_AddBoolToObject(pJsonRoot, "bool", 1);
cJSON * pSubJson = NULL;
pSubJson = cJSON_CreateObject();
if(NULL == pSubJson)
{
// create object faild, exit
cJSON_Delete(pJsonRoot);
return NULL;
}
cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");
cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);
char * p = cJSON_Print(pJsonRoot);
// else use :
// char * p = cJSON_PrintUnformatted(pJsonRoot);
if(NULL == p)
{
//convert json list to string faild, exit
//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coremp, and error is : double free
cJSON_Delete(pJsonRoot);
return NULL;
}
//free(p);
cJSON_Delete(pJsonRoot);
return p;
}
void parseJson(char * pMsg)
{
if(NULL == pMsg)
{
return;
}
cJSON * pJson = cJSON_Parse(pMsg);
if(NULL == pJson)
{
// parse faild, return
return ;
}
// get string from json
cJSON * pSub = cJSON_GetObjectItem(pJson, "hello");
if(NULL == pSub)
{
//get object named "hello" faild
}
printf("obj_1 : %s
", pSub->valuestring);
// get number from json
pSub = cJSON_GetObjectItem(pJson, "number");
if(NULL == pSub)
{
//get number from json faild
}
printf("obj_2 : %d
", pSub->valueint);
// get bool from json
pSub = cJSON_GetObjectItem(pJson, "bool");
if(NULL == pSub)
{
// get bool from json faild
}
printf("obj_3 : %d
", pSub->valueint);
// get sub object
pSub = cJSON_GetObjectItem(pJson, "subobj");
if(NULL == pSub)
{
// get sub object faild
}
cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");
if(NULL == pSubSub)
{
// get object from subject object faild
}
printf("sub_obj_1 : %s
", pSubSub->valuestring);
cJSON_Delete(pJson);
}
int main()
{
char * p = makeJson();
if(NULL == p)
{
return 0;
}
printf("%s ", p);
parseJson(p);
free(p);//这里不要忘记释放内存,cJSON_Print()函数或者cJSON_PrintUnformatted()产生的内存,使用free(char *)进行释放
return 0;
}
2)创建json数组和解析json数组
//创建数组,数组值是另一个JSON的item,这里使用数字作为演示
char * makeArray(int iSize)
{
cJSON * root = cJSON_CreateArray();
if(NULL == root)
{
printf("create json array faild ");
return NULL;
}
int i = 0;
for(i = 0; i < iSize; i++)
{
cJSON_AddNumberToObject(root, "hehe", i);
}
char * out = cJSON_Print(root);
cJSON_Delete(root);
return out;
}
//解析刚刚的CJSON数组
void parseArray(char * pJson)
{
if(NULL == pJson)
{
return ;
}
cJSON * root = NULL;
if((root = cJSON_Parse(pJson)) == NULL)
{
return ;
}
int iSize = cJSON_GetArraySize(root);
for(int iCnt = 0; iCnt < iSize; iCnt++)
{
cJSON * pSub = cJSON_GetArrayItem(root, iCnt);
if(NULL == pSub)
{
continue;
}
int iValue = pSub->valueint;
printf("value[%2d] : [%d] ", iCnt, iValue);
}
cJSON_Delete(root);
return;
}
❷ json对象中含有数组,数组中又含有数组如何用C#解析求实例
c#中解析json对象建议你使用Newtonsoft.Json
你可以用nuget获得
usingNewtonsoft.Json;
varjsonStr="";
varresult=JsonConvert.DeserializeObject<Model>(jsonStr);
其中Model可以自动生成
❸ cjson数组如何解析
JSONObject Gson Xstream等等的类库都可以解析
请把你的json代码提交上来JSONObject object = new JSONObject(json);JSONObject root = object.getJSONObject("root"); JSONObject data = root.getJSONObject("data"); JSONArray list = data.getJSONArray("list");for(int i=0; i<list.length(); i++){ JSONObject entityObj = list.getJSONObject(i); JSONArray entitys = entityObj.getJSONArray("entity"); for(int j=0; j<entitys.length(); j++){ JSONObject entity = entitys.getJSONObject(j); String date = entity.getString("取消日期"); } }引入jar,替换你的json直接可运行
❹ c#解析JSON的几种办法
对比
❺ C#解析json数组,查询是否有某个值得键。
你的json格式有点问题,应该是不完整且键名不能相同。
下面我写了个相同的程序,希望能符合你的要求,代码如下:
publicclassKeysInfo{
publicKeysInfo(){}
[JsonIgnore]
publicstringTest{get;set;}
publicMyKeys[]mykeys{get;set;}
}
publicclassMyKeys
{
publicstringkey0{get;set;}
publicstringkey1{get;set;}
publicstringkey2{get;set;}
publicstringkey3{get;set;}
publicstringkey4{get;set;}
publicstringkey5{get;set;}
publicstringkey6{get;set;}
publicstringkey7{get;set;}
}
classProgram
{
staticvoidMain(string[]args)
{
stringmyJson="{"Test":"2015","MyKeys":[{"key0":"a","key1":"b","key2":"c","key3":"d"},{"key4":"aa","key5":"bb","key6":"cc","key7":"dd"}]}";
//反序列化json对象
KeysInfodes=JsonConvert.DeserializeObject<KeysInfo>(myJson);
if(IsExist(des,"key4","aa"))Console.WriteLine("存在. ");
elseConsole.WriteLine("不存在. ");
Console.ReadKey(true);
}
publicstaticboolIsExist(KeysInfokeyInfo,stringkeyName,stringkeyValue)
{
for(inti=0;i<keyInfo.mykeys.Count();i++)
{
//反射KeysInfo类的属性以及值
foreach(System.Reflection.PropertyInfopiinkeyInfo.mykeys[i].GetType().GetProperties())
{
if(pi.Name==keyName&&(pi.GetValue(keyInfo.mykeys[i])!=null
&&pi.GetValue(keyInfo.mykeys[i]).ToString()==keyValue))
returntrue;//给定的键名与值存在于json对象
}
}
returnfalse;//给定的键名与值不存在于json对象
}
}
❻ 这种json怎么解析[ "1", "2", "3", "4" ]
int cjson_parse(char *json_data)
{
cJSON *json,*item;
int i;
json=cJSON_Parse(json_data);
if (!json)
{
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
return -1;
}
else
{
int size=cJSON_GetArraySize(json);
for(i=0;i<size;i++)
{
item=cJSON_GetArrayItem(json,i);
//atoi(item->valuestring)等到整数,
}
}
}
————————————————
版权声明:本文为CSDN博主「hairubb」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hairubb/article/details/102778242
❼ c#后台解析json数组,该怎么解决
使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/)。下载后加入工程就能用。通常可以使用JObject, JsonReader, JsonWriter处理。这种方式最通用,也最灵活,可以随时修改不爽的地方。
string jsonArrayText1 = "[{'a':'a1','b':'b1'},{'a':'a2','b':'b2'}]";
JArray ja = (JArray)JsonConvert.DeserializeObject(jsonArrayText1);
string ja1a = ja[1]["a"].ToString();
//或者
JObject o = (JObject)ja[1];
string oa = o["a"].ToString();
❽ JSON中数组该如何解析呢c++中使用jsoncpp
JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择
JSON的全称为:javaScript Object Notation ,顾名思义,JSON是用于标记javascript对象的,详情参考http://www.json.org/。
本文选择第三方库JsonCpp来解析json,JsonCpp是比较出名的c++解析库,在json官网也是首推的。
JsonCpp简介
JsonCpp主要包含三种类型的class:Value Reader Writer。
jsoncpp中所有对象、类名都在namespace json中,包含json.h即可。
注意: Json::Value只能处理ANSI类型的字符串,如果C++程序使用Unicode编码的,最好加一个Adapt类来适配。
下载和编译
本文运行环境是: Redhat 5.5 + g++version 4.6.1 + GNU Make 3.81 + jsoncpp-0.5.0
下载地址是:http://sourceforge.net/projects/jsoncpp/
解压之后得到jsoncpp-src-0.5.0文件夹,我们只需要jsoncpp的头文件和cpp文件,其中jsonscpp的头文件位于jsoncpp-src-0.5.0includejson,jsoncpp的cpp文件位于jsoncpp-src-0.5.0srclib_json。
这里我列出我们的工作目录:
jsoncpp/ //工作目录
|-- include //头文件根目录
| |-- json //json头文件,对应jsoncpp-src-0.5.0includejson
|-- src //cpp源码文件根目录
|-- json //jsoncpp源码文件,对应jsoncpp-src-0.5.0srclib_json
|-- main.cpp //我们的主函数,调用jsoncpp的示例代码
|-- makefile //makefile,不用我们多说了吧,不懂请看我博客的makefile最佳实践
反序列化Json对象
假设有一个json对象如下:
{ "name": "json″, "array": [ { "cpp": "jsoncpp" }, { "java": "jsoninjava" }, { "php": "support" } ] }
我们要实现这个json的反序列号代码如下:
voidreadJson() { usingnamespacestd; std::stringstrValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}"; Json::Reader reader; Json::Value value; if(reader.parse(strValue, value)) { std::stringout= value["name"].asString(); std::cout <<out<<std::endl; constJson::Value arrayObj = value["array"]; for(unsigned inti = 0;i <arrayObj.size(); i++) { if(!arrayObj[i].isMember("cpp")) continue; out= arrayObj[i]["cpp"].asString(); std::cout <<out; if(i != (arrayObj.size() - 1)) std::cout <<std::endl; } } }
序列化Json对象
voidwriteJson() { usingnamespacestd; Json::Value root; Json::Value arrayObj; Json::Value item; item["cpp"] = "jsoncpp"; item["java"] = "jsoninjava"; item["php"] = "support"; arrayObj.append(item); root["name"] = "json"; root["array"] = arrayObj; root.toStyledString(); std::stringout= root.toStyledString(); std::cout <<out<<std::endl; }
❾ 用C语言解析JSON数据
http://www.json.org/
列出抄了袭一堆C语言的JSON库。
C:
JSON_checker.
YAJL.
js0n.
LibU.
json-c.
json-parser.
jsonsl.
WJElement.
M's JSON parser.
cJSON.
Jansson.
jsmn.
cson.
parson.
ujson4c.
nxjson.
frozen.
❿ c# 如何解析json格式为{"a":"0","b":{"c":"0"}}这样的啊
string str="{'nodeID':'2','text':'Gxxxxxxx','path':'1111/111/2','attributes':{'nodeType':'async'}}"; //str为你的json传过来的字符串,我这边调试呢,所以里面用的'引号
str = str.Substring(1,str.Length-2);//去除两边的大括号
string[] str1 = str.Split(',');//按,分组,现在str1就是把你的字符串分成了一维数组
string[] str2;
List<string[]> strs = new List<string[]>();
for (int i = 0; i < str1.Length; i++)
{
str2 = str1[i].Split(':');
strs.Add(str2);//这里分成二维数组
}
如果你不要传过来字符串的“引号,就用replace一下