本文目录一览:
- 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)。