[英]How to get a value from JSON using JavaScript
Here we have a JSON file:这里我们有一个 JSON 文件:
[
{
"title":"Potato and Cheese Frittata",
"href":"http://allrecipes.com/Recipe/Potato-and-Cheese-Frittata/Detail.aspx",
"ingredients":"cheddar cheese, eggs, olive oil, onions, potato, salt",
"thumbnail":"http://img.recipepuppy.com/2.jpg"
},
{
"title":"Eggnog Thumbprints",
"href":"http://allrecipes.com/Recipe/Eggnog-Thumbprints/Detail.aspx",
"ingredients":"brown sugar, butter, butter, powdered sugar, eggs, flour, nutmeg, rum, salt, vanilla extract, sugar",
"thumbnail":"http://img.recipepuppy.com/3.jpg"
},
{
"title":"Irish Champ",
"href":"http://allrecipes.com/Recipe/Irish-Champ/Detail.aspx",
"ingredients":"black pepper, butter, green onion, milk, potato, salt",
"thumbnail":"http://img.recipepuppy.com/5.jpg"
},
{
"title":"Chocolate-Cherry Thumbprints",
"href":"http://allrecipes.com/Recipe/Chocolate-Cherry-Thumbprints/Detail.aspx",
"ingredients":"cocoa powder, baking powder, butter, eggs, flour, oats, salt, sugar, vanilla extract",
"thumbnail":"http://img.recipepuppy.com/6.jpg"
},
{
"title":"Hot Spiced Cider",
"href":"http://allrecipes.com/Recipe/Hot-Spiced-Cider/Detail.aspx",
"ingredients":"allspice, apple cider, brown sugar, cinnamon, cloves, nutmeg, orange, salt",
"thumbnail":"http://img.recipepuppy.com/8.jpg"
}
]
How can i get only the titles
of each results
for example here I need to get only ( Potato and Cheese Frittata, Eggnog Thumbprints, Chocolate-Cherry Thumbprints,Hot Spiced Cider)我怎样才能只得到每个
results
的titles
,例如这里我只需要得到(土豆和奶酪煎蛋饼、蛋酒指纹、巧克力樱桃指纹、辣味苹果酒)
Thank you!谢谢!
First, JSON is a string notation used for serializing information.首先,JSON 是用于序列化信息的字符串表示法。 It can be deserialized (parsed in JavaScript) into an object or array.
它可以反序列化(在 JavaScript 中解析)为对象或数组。 What you have is the deserialized format, and as a result it is simply a plain array of objects.
您拥有的是反序列化格式,因此它只是一个简单的对象数组。
As a result, the native .map
MDN api function will work to map the .title from each object into an array of just those titles.因此,原生的
.map
MDN api 函数将把每个对象的 .title 映射到一个包含这些标题的数组中。
var titles = result.map(function(obj){ return obj.title; });
Try this it will give you what you want:试试这个它会给你你想要的:
var a = [{ "title":"Potato and Cheese Frittata"}, {"title":"Eggnog Thumbprints" }];
for (var i = 0; i < a.length; i++) {
alert(a[i].title);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.