如何将一个变量中的一些 json 数组拆分为不同的变量?(Javascript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10579018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to split some json arrays in one variable into different variables? (Javascript)
提问by user1258134
I'm confused about JSON.
我对 JSON 感到困惑。
I got this JSON data (already parsed) from php;
我从 php 得到了这个 JSON 数据(已经解析);
var json =
{"id":"1", "name":"AAA", "sex":"m", "age":"20", "region":"X"}
{"id":"2", "name":"BBB", "sex":"m", "age":"25", "region":"Y"}
{"id":"3", "name":"CCC", "sex":"f", "age":"30", "region":"Z"}
{"id":"4", "name":"DDD", "sex":"m", "age":"35", "region":"Q"}
and these are separated arrays within one var.
这些是一个 var 中的分隔数组。
I'd like to make them like this,
我想让他们像这样,
var json1 = {"id":"1", "name":"AAA", "sex":"m", "age":"20", "region":"X"}
var json2 = {"id":"2", "name":"BBB", "sex":"m", "age":"25", "region":"Y"}
var json3 = {"id":"3", "name":"CCC", "sex":"f", "age":"30", "region":"Z"}
var json4 = {"id":"4", "name":"DDD", "sex":"m", "age":"35", "region":"Q"}
I'm currently developing Titanium mobile (iOS) with PHP. and PHP send JSON data like this;
我目前正在使用 PHP 开发 Titanium mobile (iOS)。和 PHP 发送这样的 JSON 数据;
foreach($responses as $response)
{
echo encode_json($response);
}
※$responses is result from sql order.
※$responses 是 sql 命令的结果。
and now Titanium side, Titanium file will receive and parse it.
现在在 Titanium 端,Titanium 文件将接收并解析它。
Req.onload = function()
{
var json = JSON.stringify(this.responseText);
var response = JSON.parse(json);
Ti.API.info(response);
}
Ti.API.info says on console;
Ti.API.info 在控制台上说;
[INFO] {"id":"1", "name":"AAA", "sex":"m", "age":"20", "region":"X"}{"id":"2", "name":"BBB", "sex":"m", "age":"25", "region":"Y"}{"id":"3", "name":"CCC", "sex":"f", "age":"30", "region":"Z"}{"id":"4", "name":"DDD", "sex":"m", "age":"35", "region":"Q"}
//as I wrote it above
I'm sorry for those came to see JS problem but Titanium.
我很抱歉那些来看 JS 问题但 Titanium 的人。
回答by Rick Hoving
Do you mean something like this?:
你的意思是这样的吗?:
var json =
{"id":"1", "name":"AAA", "sex":"m", "age":"20", "region":"X"}
{"id":"2", "name":"BBB", "sex":"m", "age":"25", "region":"Y"}
{"id":"3", "name":"CCC", "sex":"f", "age":"30", "region":"Z"}
{"id":"4", "name":"DDD", "sex":"m", "age":"35", "region":"Q"}
var json1 = json[0];
var json2 = json[1];
var json3 = json[2];
var json4 = json[3];
回答by gion_13
If your first json is actually an array like this :
如果您的第一个 json 实际上是这样的数组:
var json = [
{"id":"1", "name":"AAA", "sex":"m", "age":"20", "region":"X"},
{"id":"2", "name":"BBB", "sex":"m", "age":"25", "region":"Y"},
{"id":"3", "name":"CCC", "sex":"f", "age":"30", "region":"Z"},
{"id":"4", "name":"DDD", "sex":"m", "age":"35", "region":"Q"}
];
Then you could split the array into individual variables like this :
然后你可以将数组拆分成这样的单个变量:
for(var i=0,l=json.length;i<l;i++)
window['json' + (i+1)] = json[i];
Note that declaring a variable in the global scope (like var foo = 'bar'
) can be done by attaching that name to the window object (window['foo'] = 'bar';
);
请注意,var foo = 'bar'
可以通过将该名称附加到窗口对象 ( window['foo'] = 'bar';
)来在全局范围内声明变量(如);