本文目錄一覽:
- 1、怎麼給後端返回的json中添加數據
- 2、如何向cjson數據的object中添加值為true
- 3、js如何使用for循環往json添加數據?
- 4、js中如何向json數組添加元素
- 5、請問json如何追加內容,並且修改,和刪除操作。急~
- 6、怎麼用C語言獲取JSON中的數據?
怎麼給後端返回的json中添加數據
(1) 將如下代碼copy之後保存為 server.js
(2) 然後執行: node server.js
var http = require(‘http’);
var url = require(‘url’);
// 訪問的json地址與返回的json數據映射關係
var array=[
{
url:’/signup/index.json’,
json:'{“tasks”:[{“finishType”:”人數優先”,”gmtEnd”:”2015-11-19 11:30:00″,”gmtStart”:”2015-11-17 11:30:00″,”id”:98,”memo”:”招新任務01″,”name”:”招新任務01″,”requireNum”:10,”signedupNum”:0,”signupStatus”:””,”taskStatus”:”進行中”}],”stat”:”ok”}’
},
{
url:’/signup/applyCheck.json’,
json:'{“signupInfo”:{“alipayAccount”:”20881021179902510156″,”alipayCardNo”:”2088102117990251″,”birthday”:””,”certifyStatus”:null,”city”:””,”college”:””,”email”:”rjmuqiang@gmail.com”,”gender”:null,”gmtCreate”:null,”gmtModified”:null,”id”:2,”identityCardNo”:”330283198903120025″,”identityCardPic”:””,”major”:””,”maxWeekHours”:0,”minWeekHours”:0,”mobile”:”18905818799″,”province”:””,”qualificationPic”:””,”realName”:”zhulu”,”recruitSource”:””,”signupTaskId”:98,”status”:null,”statusReason”:””,”student”:false,”testScore”:0,”trainScore”:0},”checkResult”:{“code”:”SUCCESS”,”message”:”處理成功”,”printResult”:true,”success”:true},”stat”:”ok”}’
},
{
url:’/server/matchSuggest.json’,
json:'{“TotalHits”:88,”errorCode”:0,”errorDesc”:”no_error”,”MatchResults”:[{“CatId”:”4098″,”CatTitlePath”:”%E5%86%85%E9%83%A8%E7%9F%A5%E8%AF%86%E5%BA%93%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%E5%8F%8A%E8%B5%84%E4%BA%A7%E7%AE%A1%E7%90%86%EF%BC%88%E6%96%B0%EF%BC%89%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%3E%E5%AF%86%E7%A0%81%3E%E6%89%8B%E5%8A%BF%E5%AF%86%E7%A0%81″,”ChannelNames”:””,”Content”:””,”CreatorName”:”%E6%A1%83%E7%98%B4″,”GmtCreate”:”2015-04-07 17:58:42″,”GmtModified”:”2015-05-19 11:48:36″,”Id”:”6056″,”Keywords”:””,”ModifierId”:”12484″,”ModifierName”:”%E7%89%A7%E6%9A%AE”,”Status”:”PUBLISHED”,”Title”:”%E9%80%9A%E8%BF%87%E6%94%AF%E4%BB%98%E5%AE%9D%E9%92%B1%E5%8C%85%EF%BC%8C%3Cfont+color%3Dred%3E%E5%BF%98%E8%AE%B0%3C%2Ffont%3E%E6%89%8B%E5%8A%BF%3Cfont+color%3Dred%3E%E5%AF%86%E7%A0%81%3C%2Ffont%3E%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B”,”Type”:”NORMAL”,”deleted”:”N”}]}’
},
{
url:’/signup/signup.json’,
json:'{“stat”:”ok”}’
}
];
// var temResult='{“TotalHits”:88,”errorCode”:0,”errorDesc”:”no_error”,”MatchResults”:[{“CatId”:”4098″,”CatTitlePath”:”%E5%86%85%E9%83%A8%E7%9F%A5%E8%AF%86%E5%BA%93%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%E5%8F%8A%E8%B5%84%E4%BA%A7%E7%AE%A1%E7%90%86%EF%BC%88%E6%96%B0%EF%BC%89%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%3E%E5%AF%86%E7%A0%81%3E%E6%89%8B%E5%8A%BF%E5%AF%86%E7%A0%81″,”ChannelNames”:””,”Content”:”%e7%87%95%e5%ad%90″,”CreatorName”:”%E6%A1%83%E7%98%B4″,”GmtCreate”:”2015-04-07 17:58:42″,”GmtModified”:”2015-05-19 11:48:36″,”Id”:”6056″,”Keywords”:””,”ModifierId”:”12484″,”ModifierName”:”%E7%89%A7%E6%9A%AE”,”Status”:”PUBLISHED”,”Title”:”%E9%80%9A%E8%BF%87%E6%94%AF%E4%BB%98%E5%AE%9D%E9%92%B1%E5%8C%85%EF%BC%8C%3Cfont+color%3Dred%3E%E5%BF%98%E8%AE%B0%3C%2Ffont%3E%E6%89%8B%E5%8A%BF%3Cfont+color%3Dred%3E%E5%AF%86%E7%A0%81%3C%2Ffont%3E%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B”,”Type”:”NORMAL”,”deleted”:”N”},{“CatId”:”4098″,”CatTitlePath”:”%E5%86%85%E9%83%A8%E7%9F%A5%E8%AF%86%E5%BA%93%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%E5%8F%8A%E8%B5%84%E4%BA%A7%E7%AE%A1%E7%90%86%EF%BC%88%E6%96%B0%EF%BC%89%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%3E%E5%AF%86%E7%A0%81%3E%E6%89%8B%E5%8A%BF%E5%AF%86%E7%A0%81″,”ChannelNames”:””,”Content”:”%e7%87%95%e7%aa%9d”,”CreatorName”:”%E6%A1%83%E7%98%B4″,”GmtCreate”:”2015-04-07 17:58:42″,”GmtModified”:”2015-05-19 11:48:36″,”Id”:”6056″,”Keywords”:””,”ModifierId”:”12484″,”ModifierName”:”%E7%89%A7%E6%9A%AE”,”Status”:”PUBLISHED”,”Title”:”%E9%80%9A%E8%BF%87%E6%94%AF%E4%BB%98%E5%AE%9D%E9%92%B1%E5%8C%85%EF%BC%8C%3Cfont+color%3Dred%3E%E5%BF%98%E8%AE%B0%3C%2Ffont%3E%E6%89%8B%E5%8A%BF%3Cfont+color%3Dred%3E%E5%AF%86%E7%A0%81%3C%2Ffont%3E%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B”,”Type”:”NORMAL”,”deleted”:”N”}]}’;
http.createServer(function(request, response){
response.writeHead(200,{
“Access-Control-Allow-Origin”:””,
“Access-Control-Allow-Credentials”: “true”,
“Access-Control-Allow-Headers”:”X-Requested-With”,
“Access-Control-Allow-Methods”:”PUT,POST,GET,DELETE,OPTIONS”,
“X-Powered-By”:”3.2.1″,
“Content-Type”:”application/json;charset=utf-8″,
“Connection”:”keep-alive”
});
var reqURL=request.url;
var result=reqURL + ” 對應的json結果數據是什麼?請進行配置”;
var i=array.length;
while(i–){
if(reqURL.indexOf(array[i].url)==0){
result= array[i].json;
}
console.log(111);
}
// var params = url.parse(request.url, true).query;
// console.log(params);
response.write(result);
response.end();
}).listen(8787);
console.log(‘啟動成功…’);
如何向cjson數據的object中添加值為true
這個明顯是WinForm的吧。 1、新建資料庫 USE [master] GO /****** Object: Database [資料庫名] Script Date: 09/12/2014 16:56:48 ******/ CREATE DATABASE [資料庫名] ON PRIMARY ( NAME = N’資料庫名’, FILENAME = N’D:\Program Files\Micros…
js如何使用for循環往json添加數據?
=並不會把上一條覆蓋,因為arr[i].name並不一樣,相當於給json動態添加不同的屬性。結果如下
當然,如果你數組中的那麼如果會一樣的話,那麼就會被覆蓋掉。如果是想要往一個新的對象上加,當作它的屬性,那麼屬性名肯定是唯一的。如果你只是單純的先把數組變成json串的話,可以使用JSON.stringify();,如下
js中如何向json數組添加元素
比如現在有一個json對象為jsonObj,需要給這個對象添加新的屬性newParam,同時給newParam賦值為pre。做法如下:
var jsonObj={
‘param1’:22,
‘param2’ :33
};
jsonObj. newParam =’pre’;
新的屬性添加以後,json對象變成:
var jsonObj={
‘param1’:22,
‘param2’ :33,
‘newParam’:’pre’
};
擴展資料:
json數據格式:主要由對象 { } 和數組 [ ] 組成:
其中對象包括鍵值對(屬性:屬性值){key: value},value 可為 str,num,list,obj。取值使用 objcet.key。
{key: value, key2:value2,} 鍵:值用冒號分開,對間用,連接。
數組包含元素:num,str,list,objcet 都可以,利用索引訪問 [index],用 . 連接各個值。
請問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
怎麼用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)。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/243836.html