json数组对象如何取出其中一部分需要的字段,组成新数组对象

//服务器获得的
var data =	[
	  {dd:'22'		,AA:'东东'		,re1:123},
	  {dd:'33'		,AA:'丽丽'		,re1:234},
	  {dd:'44'		,AA:'明明'		,re1:456}
	];
//理想的
[
  {dd:'22'			,re1:123},
  {dd:'33'			,re1:234},
  {dd:'44'			,re1:456}
];

eg:只需要dd,rel字段。我们需要剔除AA字段

实现:

var arr = [];
//遍历
data.forEach(function(item){
 arr.push({id: item.dd,	 text: item.re1})
})
console.log(arr)

//打印结果:
[
{id:‘22’ ,text:123},
{id:‘33’ ,text:234},
{id:‘44’ ,text:456}
];

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个使用结构体写出json格式的示例,其中包含一个数组: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char *name; int age; } Person; typedef struct { char *title; int year; Person *cast; int cast_size; } Movie; int main() { Person p1 = {"Alice", 25}; Person p2 = {"Bob", 30}; Person cast[] = {p1, p2}; Movie m = {"The Matrix", 1999, cast, 2}; char *json = malloc(1000); sprintf(json, "{\"title\":\"%s\",\"year\":%d,\"cast\":[", m.title, m.year); for (int i = 0; i < m.cast_size; i++) { sprintf(json + strlen(json), "{\"name\":\"%s\",\"age\":%d}", m.cast[i].name, m.cast[i].age); if (i < m.cast_size - 1) { strcat(json, ","); } } strcat(json, "]}"); printf("%s\n", json); free(json); return 0; } ``` 在这个示例中,我们定义了两个结构体:`Person` 和 `Movie`。`Person` 结构体表示一个人,包含姓名和年龄两个字段;`Movie` 结构体表示一部电影,包含标题、年份、演员列表和演员数量四个字段。我们使用结构体对象 `m` 来表示一部电影,其中演员列表是一个 `Person` 类型的数组。 我们使用 `sprintf` 函数来将结构体对象 `m` 转换成 json 格式的字符串。首先,我们将电影的标题和年份转换成 json 字符串的一部分,然后遍历演员列表,将每个演员转换成 json 字符串的一部分,并将它们拼接在一起。最后,我们将整个 json 字符串打印出来。 如果我们想要使用结构体对象来调用这个 json 字符串,可以使用第三方库,如 cJSON

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值