本文目錄一覽:
- 1、C#如何將窗體上的treeview控件內容全部寫入json文件?
- 2、ASP 讀取json後如何 寫入數據庫
- 3、c++保存數組到json文件報錯?
- 4、怎麼用C語言獲取JSON中的數據?
- 5、請問json如何追加內容,並且修改,和刪除操作。急~
- 6、lua中怎樣將json文件寫入沙盒
C#如何將窗體上的treeview控件內容全部寫入json文件?
從內存中將數據保存到文件的過程叫持久化,持久化可以用序列化的方式來實現
序列化和反序列化可以用Json.NET
File.WriteAllText(@”c:\movie.json”, JsonConvert.SerializeObject(treeview1));
但是這樣序列化可能會有引用的問題,更好的方式是你只將bind到treeview的數據源序列化
然後寫一個加載數據的方法,這樣只用讀寫真正需要的數據,而不用管treeview的問題
ASP 讀取json後如何 寫入數據庫
ASP 獲取JSON 數據:script language=”JScript” runat=”Server”
function toObject(json) {
eval(“var o=” + json);
return o;
}
/script
%
Dim json
json =”{“”px_name””:””第二屆””,””px_ksjs””:””2014-03-11″”,””px_kcfl””:””培訓課程””}”
Set json = toObject(json)
Response.Write json.px_name ” br/”
Response.Write json.px_ksjs ” br/”
Response.Write json.px_kcfl ” br/”
Set json = Nothing
%
2.寫入數據庫
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|db1.mdb”;
OleDbCommand comm = new OleDbCommand();
conn.Open();
comm.Connection = conn;
comm.CommandText = “Insert Into [Time] ([begin],[over],[name]) Values (@begin,@over,@name)”;
comm.Parameters.AddWithValue(“@begin”, a);
comm.Parameters.AddWithValue(“@over”, b);
comm.Parameters.AddWithValue(“@name”, c);
comm.ExecuteNonQuery();
conn.Close();
注意:
1、以上代碼是以access數據為例。
2、如果表裡面有其他不能為空的字段存在,需要給他們提供值,自動增加的字段除外。
3、db1.mdb文件的只讀屬性去掉,在文件的屬性-安全性裡面,添加 everyone帳號和NETWORK Service 帳號可修改權限。
c++保存數組到json文件報錯?
不能一次放入過多數據,建議循環寫入數據,不要一次性寫入。望採納,謝謝
怎麼用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 coredump, 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\n”, pSub-valuestring);
// get number from json
pSub = cJSON_GetObjectItem(pJson, “number”);
if(NULL == pSub)
{
//get number from json faild
}
printf(“obj_2 : %d\n”, pSub-valueint);
// get bool from json
pSub = cJSON_GetObjectItem(pJson, “bool”);
if(NULL == pSub)
{
// get bool from json faild
}
printf(“obj_3 : %d\n”, 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\n”, pSubSub-valuestring);
cJSON_Delete(pJson);
}
int main()
{
char * p = makeJson();
if(NULL == p)
{
return 0;
}
printf(“%s\n”, 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\n”);
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]\n”, iCnt, iValue);
}
cJSON_Delete(root);
return;
}
有兩種方法:
一是標準的輸出輸入方式 比如新建一個磁盤文件c:\a.txt, 將鍵盤輸入的一字符串寫到文件中:
FILE *ft;
char str[50];
ft=fopen(“c:\\a.txt”,”w+”);
printf(“輸入一個字符串:”);
scanf(“%s”,str);
fputs(str,ft);
fclose(ft);
//重新打開這個文件並讀出字符串,顯示在屏幕上 ft=fopen(“c:\\a.txt”,”rt”);
fgets(str,50,ft);
fclose(ft); printf(“%s”,str);
二是低級輸入輸出方式 仍如上例:
int hd; char str[50]; printf(“輸入一個字符串:”);
scanf(“%s”,str);
hd=open(“c:\\a.txt”,O_CREAT|O_TEXT|O_WRONLY);
write(hd,str,strlen(str));
close(hd); //重新打開這個文件並讀出字符串,顯示在屏幕上。
hd=open(“c:\\a.txt”,O_TEXT|O_RDONLY); read(hd,str,50);
close(hd); printf(“%s”,str)。
請問json如何追加內容,並且修改,和刪除操作。急~
json追加內容並且修改和刪除操作示例:
1、增加:
myObj.user=’我是新增的用戶-小明’;
x +=”h1增加後的數據/h1″forin();
2、修改:
myObj.name= “我的網站”;
x +=”h1修改後的數據/h1″forin();
3、刪除:
delete myObj.sites;
x +=”h1刪除後的數據/h1″forin();
擴展資料
JS動態動態創建JSON數據字符串,並且可以刪除添加修改
script type=”text/javascript”
//添加或者修改json數據
function setJson(jsonStr,name,value)
{
if(!jsonStr)jsonStr=”{}”;
var jsonObj = JSON.parse(jsonStr);
jsonObj[name] = value;
return JSON.stringify(jsonObj)
}
//刪除數據
function deleteJson(jsonStr,name)
{
if(!jsonStr)return null;
var jsonObj = JSON.parse(jsonStr);
delete jsonObj[name];
return JSON.stringify(jsonObj)
}
//生成測試
var myjsonStr = setJson(null,”name”,”aaa”);
alert(myjsonStr);
//添加測試
myjsonStr = setJson(myjsonStr,”age”,18);
alert(myjsonStr);
//修改測試
myjsonStr = setJson(myjsonStr,”age”,20);
alert(myjsonStr);
//刪除測試
myjsonStr = deleteJson(myjsonStr,”age”);
alert(myjsonStr);
/script
lua中怎樣將json文件寫入沙盒
解析JSON
local cjson = require “cjson”
local sampleJson = [[{“age”:”23″,”testArray”:{“array”:[8,9,11,14,25]},”Himi”:”himigame.com”}]];
–解析json字符串
local data = cjson.decode(sampleJson);
–打印json字符串中的age字段
print(data[“age”]);
–打印數組中的第一個值(lua默認是從0開始計數)
print(data[“testArray”][“array”][1]);
編碼JSON
local cjson = require “cjson”
local retTable = {}; –最終產生json的表
–順序數值
local intDatas = {};
intDatas[1] = 100;
intDatas[2] = “100”;
–數組
local aryDatas = {};
aryDatas[1] = {};
aryDatas[1][“鍵11”] = “值11”;
aryDatas[1][“鍵12”] = “值12”;
aryDatas[2] = {};
aryDatas[2][“鍵21”] = “值21”;
aryDatas[2][“鍵22”] = “值22”;
–對Table賦值
retTable[“鍵1”] = “值1”;
retTable[2] = 123;
retTable[“int_datas”] = intDatas;
retTable[“aryDatas”] = aryDatas;
–將表數據編碼成json字符串
local jsonStr = cjson.encode(retTable);
print(jsonStr);
–結果是:{“int_datas”:[100,”100″],”2″:123,”鍵1″:”值1″,”aryDatas”:[{“鍵12″:”值12″,”鍵11″:”值11”},{“鍵21″:”值21″,”鍵22″:”值22”}]}。
原創文章,作者:QFAG,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/140377.html