typescript 在打字稿中将 JSON 转换为字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37219724/
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
convert a JSON to a string array in typescript
提问by malachi54
I feel like I have quite an easy question but I just don't get to the answer...
I parse the .text() of a response from my server with JSON.parse into JSON.
And i actually can access the values of that JSON with: this.temp[0].name
.
Now i just want to iterate over that JSON and push the values into a string array. But how can i achieve this?
我觉得我有一个很简单的问题,但我只是没有得到答案......我用 JSON.parse 将来自我的服务器的响应的 .text() 解析为 JSON。实际上我可以访问JSON的值:this.temp[0].name
。现在我只想遍历该 JSON 并将值推送到字符串数组中。但我怎样才能做到这一点?
When i attempt to use for...of
the compiler wants a string or an array but not a JSON. When i attempt to use for...in
it actually does not go into the loop.
当我尝试使用for...of
编译器想要一个字符串或一个数组而不是一个 JSON。当我尝试使用for...in
它时,实际上并没有进入循环。
Here is some code where i can access it fine:
这是一些我可以正常访问的代码:
this._pS.getAllProjects()
.subscribe(data => this.temp = JSON.parse(data.text()),
err => console.log(err),
() => console.log("hello " + this.temp[0].name + this.temp[1].name));
When i just print it out after .stringify():
当我在 .stringify() 之后打印出来时:
this._pS.getAllProjects()
.subscribe(data => this.temp = JSON.stringify(data),
err => console.log(err),
() => console.log("hello " + this.temp));
i get this in the browser console:
我在浏览器控制台中得到了这个:
{"_body":"[{\"name\":\"Projekt A\"},{\"name\":\"Projekt XYZ\"}]",
"status":200,
"ok":true,
"statusText":"Ok",
"headers":{"Content-Type":["application/json; charset=utf-8"]},
"type":2,
"url":"http://127.0.0.1:3000/api/getAllProjectNames"}
tl;dr: how can i get the values out of text part of the body into a string array?
tl; dr:如何将正文的文本部分中的值提取到字符串数组中?
Thanks in advance
提前致谢
回答by Nitzan Tomer
If the response is what you included then it's simple:
如果响应是您包含的内容,则很简单:
JSON.parse(data.text()).forEach(item => {
console.log(item.name);
});
As your body is an array of objects of this interface:
由于您的身体是此接口的对象数组:
interface ObjectInResponseArray {
name: string;
}
回答by Picci
I would try something like
我会尝试类似的东西
this._pS.getAllProjects()
.subscribe(resp => {
this.temp = resp.json();
console.log("hello " + this.temp[0].name + this.temp[1].name);
},
err => console.log(err),
() => console.log("getAllProjectsDone")
)
I hope this helps
我希望这有帮助
回答by kemsky
Use map
:
使用map
:
this._pS.getAllProjects()
.map(resp => resp.json())
.map(json => json.map(project => project.name)).subscribe(...);
回答by Mike Cluck
First off, you're mixing up JSON and objects. JSON is a text format for transferring data. Once you parse it (JSON.parse
) you've got an object or an array, depending on the contents of the JSON.
首先,您将 JSON 和对象混为一谈。JSON 是一种用于传输数据的文本格式。解析它 ( JSON.parse
) 后,您将获得一个对象或数组,具体取决于 JSON 的内容。
var json = "{\"key\": \"value\" }";
var object = JSON.parse(json);
json = "[1, 2, \"three\"]";
var array = JSON.parse(json);
Next, it looks like you already have an object. If you do JSON.stringify
, you're converting an object into a string. So you can access your data
object directly. No need to convert one way or the other.
接下来,您似乎已经有了一个对象。如果这样做JSON.stringify
,您就是在将对象转换为字符串。所以你可以data
直接访问你的对象。无需以一种或另一种方式转换。
console.log(data._body[0]);
Are you familiar with for
loops? They're the most common way of iterating through an array in JavaScript (and most languages, actually). For example:
你熟悉for
循环吗?它们是在 JavaScript(实际上是大多数语言)中迭代数组的最常见方式。例如:
var objs = [
{
name: 'Bugs'
},
{
name: 'Bunny'
}
];
for (var i = 0; i < objs.length; i++) {
var obj = objs[i];
console.log(obj.name);
}
Given all of that, you should be able to iterate over your data._body
array and access the name
property on each element.
鉴于所有这些,您应该能够遍历data._body
数组并访问name
每个元素的属性。
Since your question is a bit unclear, it's possible you'll need to iterate over the parsed contents of data.text()
.
由于您的问题有点不清楚,因此您可能需要遍历data.text()
.
var objs = JSON.parse(data.text());
// Use a simple loop like above