導航:首頁 > 編程語言 > c生成jsonstring

c生成jsonstring

發布時間:2021-12-09 11:15:28

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() {

}

閱讀全文

與c生成jsonstring相關的資料

熱點內容
最好看的機甲小說 瀏覽:495
小孩第一次進電影院英文翻譯 瀏覽:729
ios獲取項目文件路徑 瀏覽:100
色武俠小說 瀏覽:879
users文件夾共享 瀏覽:372
mybatis查詢大數據 瀏覽:278
染島貢電影 瀏覽:101
蘋果7黑屏指紋沒反應 瀏覽:655
如何把相冊轉成文件 瀏覽:973
pb這么獲取資料庫窗口的值 瀏覽:856
數據類型中哪些支持默認約束 瀏覽:711
裸眼3D電影左右格式下載 瀏覽:848
如何通過網路線控制連接主機 瀏覽:873
韓劇海嘯電影 瀏覽:231
韓國電影男孩在樓上偷看樓下 瀏覽:151
網址懂的都懂 瀏覽:209
日劇男的是攝影師 瀏覽:861
手機網路聯不上怎麼辦 瀏覽:965
在線免費看那種片網址 瀏覽:838
java移動圖片 瀏覽:126

友情鏈接