❶ json-c庫,json格式字元串處理問題
你把json字元串放到 eval(json字元串) 函數中,就能得到一組你想要的對象了。
❷ c語言 解析json字元串
你好,你用json-c庫,編譯通過了嗎?我是在ubuntu里使用json-c庫,但是無法編譯通過回,報錯 undefined reference to 'json_tokener_parse',類似的函數答沒定義的錯誤,你是怎麼調用的json-c庫?請教一下,謝謝!
❸ 怎麼用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;
}
❹ c語言如何將計算出的時間變數寫入JSON字元串中
char str[7]="adcdef"; 也可用struct定義一個結構變數體 typedef struct { char str[20];//用於存放字元串,20為最大容量 int count;//統計實際存放的字元個數 }string;
❺ 如何將 JsonResult的數據轉換為String
Json數據格式:
var jsontext = "[{"id":"815bb899-8d70-4745-b799-7e68840a2b34","text":"設計","pid":"57eb2d57-9384-4d9f-a399-2c228fea81c2"},{"id":"4c76b72c-35da-4749-893c-5c7dde9431e6","text":"市政工程","pid":""}]";
/// <summary>
/// Json數據轉數組
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonText"></param>
/// <returns></returns>
public List<T> JsonToList<T>(string jsonText)
{
List<T> list = new List<T>();
DataContractJsonSerializer _Json = new DataContractJsonSerializer(list.GetType());
byte[] _Using = System.Text.Encoding.UTF8.GetBytes(jsonText);
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
_MemoryStream.Position = 0;
return (List<T>)_Json.ReadObject(_MemoryStream);
}
public class CheckedTreeInfo
{
/// <summary>
/// 父節點Id
/// </summary>
public string pid { get; set; }
/// <summary>
/// 節點Id
/// </summary>
public string id { get; set; }
/// <summary>
/// 節點名稱
/// </summary>
public string text { get; set; }
}
❻ java 將一個String 形式的json轉化成Map,比如"{\"a\":\"1\",\"b\
將一個String 形式的轉化成Map的Java程序如下:
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importorg.json.JSONObject;
publicclassFFF{
publicstaticvoidmain(String[]args){
Strings="{"a":"1","b":2,"c":"2016-5"}";
JSONObjectjso=newJSONObject(s);
Map<String,Object>m=newHashMap<String,Object>();
Iteratorit=jso.keys();
while(it.hasNext()){
Stringkey=(String)it.next();
Objectvalue=jso.get(key);
m.put(key,value);
}
System.out.println(m.toString());
}
}
運行結果:
{a=1, b=2, c=2016-5}
❼ c語言如何判斷一段字元串是否是json格式的
如果你的json 不算復雜的話,可以直接用一個簡單的正則
string pattern=@"{("\w+":(\d+|"\w+"|true|false|null))+}\] "; // input 是json字元串 var match = Regex.Match(input, pattern);
如果復雜的,你需要 判斷 ":"等!
也可以用這個:
var serializer = new JavaScriptSerializer();dynamic obj = serializer.Deserialize(json, typeof(object));
//判斷 obj就行!
❽ 怎麼用 C/C++ 把結構體數組轉成 JSON串
用CSTRING的GetBuffer函數返回一個存放字元的頭指針,用一個CHAR *來接受他,然後用下標操作就可以版了。
例如:char * ptr = str.GetBuffer();
定義一個數權組來接受各個字元
cahr array[10]="0";
char array[0]=ptr[0];
char array[1]=ptr[1];
也可以用一個循環,這樣ARRAY數組就保存了CSTRING中的各個字元.
❾ C#如何生成json字元串
推薦用Newtonsoft.Json的類庫
用類庫的JsonHelper.SerializeObject()方法 參數是個類
可以將C#里的類轉化為內json數據
比如
public class test
{
public int id {get; set;}
}
class c=new test();
string json=JsonHelper.SerializeObject(c);
注意using類庫
滿意請容採納 謝謝
❿ 如何把json對象轉換成字元串
1.簡單的解析json字元串
首先將json字元串轉換為json對象,然後再解析json對象,過程如下。
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
[java] view plain
<pre></pre><span style="white-space:pre"></span>
<pre></pre>
根據json中的鍵得到它的值
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");
2.將json字元串轉換為java對象
同樣先將json字元串轉換為json對象,再將json對象轉換為java對象,如下所示。
JSONObject obj = new JSONObject().fromObject(jsonStr);//將json字元串轉換為json對象
將json對象轉換為java對象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//將建json對象轉換為Person對象
3.將java對象轉換為json字元串
先將java對象轉換為json對象,在將json對象轉換為json字元串
JSONObject json = JSONObject.fromObject(obj);//將java對象轉換為json對象
String str = json.toString();//將json對象轉換為字元串
完整代碼如下:
[java] view plain
package baz.parse;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import baz.bean.Person;
public class ParseJson {
private String jsonStr;
public ParseJson() {
}