js json格式字符串

lanyumeng 2014-06-09 02:48:02
{
"data": {
"猎神OL_安卓": [
{
"gameId": 10,
"promptChannelId": null,
"countOfNewUsers": 100,
"startDate": null,
"endDate": null,
"createdDate": "2014-06-05",
"hourSpanType": 0,
"proportion": null,
"showedTimePattern": "00:00~~01:00"
},
{
"gameId": 10,
"promptChannelId": null,
"countOfNewUsers": 100,
"startDate": null,
"endDate": null,
"createdDate": "2014-06-05",
"hourSpanType": 1,
"proportion": null,
"showedTimePattern": "01:00~~02:00"
}
]
}

json 格式字符串 怎么取得 "猎神OL_安卓" 这个key值 多个循环,key不是固定值
还有countOfNewUsers 的值
...全文
753 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanyumeng 2014-08-15
  • 打赏
  • 举报
回复
引用 7 楼 thc1987 的回复:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <script type="text/javascript">
d = {
   "data": {
     "一组": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
     ],
"二组": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
     ]
  }
};

var series = [];

var data = d.data;

for(var key in data){
	var arr = data[key];
	
	var serieData = []; // 这里存放data

	for(var i in arr){
		// 存放
		serieData.push(arr[i].countOfNewUsers);
	}
	// 组装成对象
	var serie = {name:key,data:serieData};
	// 放进数组中
	series.push(serie);
}

// 这个就是了
console.log(series)

  </script>
 </head>

 <body>

 </body>
</html>
shizhongbugouzl 2014-06-10
  • 打赏
  • 举报
回复
有收获!菜鸟是需要成长的!
猿敲月下码 2014-06-10
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <script type="text/javascript">
d = {
   "data": {
     "一组": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
     ],
"二组": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
     ]
  }
};

var series = [];

var data = d.data;

for(var key in data){
	var arr = data[key];
	
	var serieData = []; // 这里存放data

	for(var i in arr){
		// 存放
		serieData.push(arr[i].countOfNewUsers);
	}
	// 组装成对象
	var serie = {name:key,data:serieData};
	// 放进数组中
	series.push(serie);
}

// 这个就是了
console.log(series)

  </script>
 </head>

 <body>

 </body>
</html>
lanyumeng 2014-06-09
  • 打赏
  • 举报
回复
d = {
   "data": {
     "一组": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
     ]
"二组": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
     ]
  }
};
数据是这样的,按照这样的方式 a = []; for(k in d.data) a.push(k); 取出值并且放到数组中。 这些数据是通过highcharts 作为图表数据以折线图展示出来。它所需要的结构是这样的:
series: [
			         {
			            name: '一组',
			            data: [100, 100]
			        }, {
			            name: '一组',
			            data: [100, 200, ]
			        }
data 是countOfNewUsers 某条数据的集合,能通过js 把json格式数据 弄成这种结构的吗?
xuzuning 2014-06-09
  • 打赏
  • 举报
回复
其实你只是 data 下的第一层(猎神OL_安卓)键名是不固定的 所以可以先提取出来
d = {
   "data": {
     "猎神OL_安卓": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
     ]
  }
};
a = [];
for(k in d.data) a.push(k);
alert(a);
到使用的时候 p = d.data[a[0]][1].countOfNewUsers
lanyumeng 2014-06-09
  • 打赏
  • 举报
回复
引用 楼主 lanyumeng 的回复:
{
   "data": {
     "猎神OL_安卓": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
]
}
json 格式字符串 怎么取得 "猎神OL_安卓" 这个key值 多个循环,key不是固定值 还有countOfNewUsers 的值
这样是可以获取到值,需要得到所有的值怎么办?在for循环里控制台可以输出所有的k值,可是在for之外怎么获取让函数返回所有的值
lawine9 2014-06-09
  • 打赏
  • 举报
回复
把你的数据 var data={ "data": { "猎神OL_安卓": [ ...
lawine9 2014-06-09
  • 打赏
  • 举报
回复
引用 1 楼 lawine9 的回复:
是这个意思?
var data=obj.data;
for(var k in data){
	console.log(k);//猎神OL_安卓
var arr=data[k];
	for(var i=0;i<arr.length;i++){
		var o=arr[i];
		console.log(o.countOfNewUsers);//countOfNewUsers的值
	}
}
{
   "data": {
     "猎神OL_安卓": [
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 0,
            "proportion": null,
            "showedTimePattern": "00:00~~01:00"
        },
        {
            "gameId": 10,
            "promptChannelId": null,
            "countOfNewUsers": 100,
            "startDate": null,
            "endDate": null,
            "createdDate": "2014-06-05",
            "hourSpanType": 1,
            "proportion": null,
            "showedTimePattern": "01:00~~02:00"
        }
]
}
lawine9 2014-06-09
  • 打赏
  • 举报
回复
是这个意思?
var data=obj.data;
for(var k in data){
	console.log(k);//猎神OL_安卓
var arr=data[k];
	for(var i=0;i<arr.length;i++){
		var o=arr[i];
		console.log(o.countOfNewUsers);//countOfNewUsers的值
	}
}

87,922

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧