㈠ 怎麼用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文件的數據
var json = { contry:{ area:{ man:"12萬", women:"10萬" } } };
//方式一:使用eval解析
var obj = eval(json);
alert(obj.constructor);
alert(obj.contry.area.women);
//方式二:使用Funtion函數
var strJSON = "{name:'json name'}";//得到的JSON
var obj = new Function("return" + strJSON)();//轉換後的JSON對象
alert(obj.name);//json name
alert(obj.constructor);
//復雜一點的json數組數據的解析
var value1 = [{"c01":"1","c02":"2","c03":"3","c04":"4","c05":"5","c06":"6","c07":"7","c08":"8","c09":"9"}, {"c01":"2","c02":"4","c03":"5","c04":"2","c05":"8","c06":"11","c07":"21","c08":"1","c09":"12"}, {"c01":"5","c02":"1","c03":"4","c04":"11","c05":"9","c06":"8","c07":"1","c08":"8","c09":"2"}]; var obj1 = eval(value1);
alert(obj1[0].c01);
//復雜一點的json的另一種形式
var value2 = {"list":[ {"password":"1230","username":"coolcooldool"}, {"password":"thisis2","username":"okokok"}], "array":[{"password":"1230","username":"coolcooldool"},{"password":"thisis2","username":"okokok"}]};
var obj2 = eval(value2);
alert(obj2.list[0].password);
㈢ 如何從json中取數據
首先,例如json數據如下書寫:
[html] view plain
{"head": [
{"text":"廣州","id":"廣州","pid":"廣東省"},
{"text":"鄭州","id":"鄭州","pid":"河南省"}],
}
如上,倘若想將id和pid數據依次取出,就只能用循環,代碼如下:
[html] view plain
var head_id = "";
var head_pid = "";
for (var i = 0; i < data.head.length; i++) {
head_id += data.head[i].id + " "; //循環輸出json數據
head_pid += data.head[i].pid + " ";
}
$("#city").append("city:" + head_id);
$("#city").append("province:" + head_pid);
這樣,將會在data之後,依次輸出json中的數據。
若想有選擇性的輸出時,需要添加if條件,代碼如下:
[html] view plain
for (var i = 0; i < data.head.length; i++) {
if (data.head[i].pid == "河南省") { //有選擇的輸出json數據
head_pid += data.head[i].pid;
}
}
需要注意的是,倘若對象中有多組數據,則用data.head.id是undefined的,因為沒有標明是哪組數據,例如data.head[0].id,若對象中只有一組數據,則可以直接用data.head.id輸出。
㈣ java怎麼取json數據的值
獲取JSON的值。 就是解析JSON數據.
如果是簡單的JSON數據, 並且只需要提取少量數據的值, 那麼可以使用字元串的操作來實現,比如String.subString()...等
如果是比較復雜的JSON數據,或者需要提取的值比較多, 那麼可以使用Gson, FastJSon 等第三方的jar來實現...
簡單的Demo示例
第三方包使用的是Gson
importcom.google.gson.JsonElement;
importcom.google.gson.JsonObject;
importcom.google.gson.JsonParser;
publicclassGsonTest{
publicstaticvoidmain(String[]args){
StringstrJson="{"name":"張三","age":12}";
JsonParserparser=newJsonParser();
JsonElementje=parser.parse(strJson);
JsonObjectjobj=je.getAsJsonObject();//從json元素轉變成json對象
Stringname=jobj.get("name").getAsString();//從json對象獲取指定屬性的值
System.out.println(name);
intage=jobj.get("age").getAsInt();
System.out.println(age);
}
}
㈤ 如何讀取Json文件的數據
json文件是一種輕量級的數據交互格式。一般在jquery中使用getJSON()方法讀取。
$.getJSON(url,[data],[callback])
url:載入的頁面地址
data: 可選項,發送到伺服器的數據,格式是key/value
callback:可選項,載入成功後執行的回調函數
1.首先建一個JSON格式的文件userinfo.json 保存用戶信息。如下:
?
1234567891011121314151617
[{"name":"張國立","sex":"男","email":"[email protected]"},{"name":"張鐵林","sex":"男","email":"[email protected]"},{"name":"鄧婕","sex":"女","email":"[email protected]"}]
2.其次建一個頁面用於獲取JSON文件里的用戶信息數據,並顯示
?
04142
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>getJSON獲取數據</title><script type="text/javascript" src="js/jquery-1.8.2.min.js"></script><style type="text/css">#divframe{ border:1px solid #999; width:500px; margin:0 auto;}.loadTitle{ background:#CCC; height:30px;}</style>< script type = "text/javascript" >$(function (){ $("#btn").click(function () { $.getJSON("js/userinfo.json", function (data){ var $jsontip = $("#jsonTip"); var strHtml = "123"; //存儲數據的變數 $jsontip.empty(); //清空內容 $.each(data, function (infoIndex, info){ strHtml += "姓名:" + info["name"] + "<br>"; strHtml += "性別:" + info["sex"] + "<br>"; strHtml += "郵箱:" + info["email"] + "<br>"; strHtml += "<hr>" }) $jsontip.html(strHtml); //顯示處理後的數據 }) })})</script></head><body><div id="divframe"><div class="loadTitle"><input type="button" value="獲取數據" id="btn"/></div><div id="jsonTip"></div></div></body></html>