如何从express中获取数据中的数据

时间:2015-06-04 08:07:09

标签: arrays node.js express ejs

我在nodejs中使用带有快速ejs的rest API。 从那个api我得到如下的JSON数据。 在这个数据中,数组内部有一个数组。 问题是我想要guid的feature_image值的值。但是当我试图获得该值时,它会显示has no method 'forEach错误。

[
  {
    "ID": 161,
    "title": "Compant Name 3",
    "status": "publish",
    "type": "testimonial",
    "author": {
      "ID": 1,
      "username": "admin",
      "name": "admin",
      "first_name": "",
      "last_name": "",
      "nickname": "admin",
      "slug": "admin",
      "URL": "",
      "avatar": "http://2.gravatar.com/avatar/56282b1eb6c5d293a03d2c167924b197?s=96",
      "description": "",
      "registered": "2015-04-28T10:14:02+00:00",
      "meta": {
        "links": {
          "self": "http://tryitoneyewear.com/wp-json/users/1",
          "archives": "http://tryitoneyewear.com/wp-json/users/1/posts"
        }
      }
    },
    "content": "<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem </p>\n",
    "parent": null,
    "link": "http://tryitoneyewear.com/testimonial/compant-name-3/",
    "date": "2015-06-04T07:33:29",
    "modified": "2015-06-04T08:01:26",
    "format": "standard",
    "slug": "compant-name-3",
    "guid": "http://tryitoneyewear.com/?post_type=testimonial&#038;p=161",
    "excerpt": "<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem</p>\n",
    "menu_order": 0,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "date_tz": "UTC",
    "date_gmt": "2015-06-04T07:33:29",
    "modified_tz": "UTC",
    "modified_gmt": "2015-06-04T08:01:26",
    "meta": {
      "links": {
        "self": "http://tryitoneyewear.com/wp-json/posts/161",
        "author": "http://tryitoneyewear.com/wp-json/users/1",
        "collection": "http://tryitoneyewear.com/wp-json/posts",
        "replies": "http://tryitoneyewear.com/wp-json/posts/161/comments",
        "version-history": "http://tryitoneyewear.com/wp-json/posts/161/revisions"
      }
    },
    "featured_image": {
      "ID": 28,
      "title": "dummy_img",
      "status": "inherit",
      "type": "attachment",
      "author": {
        "ID": 1,
        "username": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "slug": "admin",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/56282b1eb6c5d293a03d2c167924b197?s=96",
        "description": "",
        "registered": "2015-04-28T10:14:02+00:00",
        "meta": {
          "links": {
            "self": "http://tryitoneyewear.com/wp-json/users/1",
            "archives": "http://tryitoneyewear.com/wp-json/users/1/posts"
          }
        }
      },
      "content": "<p class=\"attachment\"><a href='http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img.jpg'><img width=\"200\" height=\"200\" src=\"http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img.jpg\" class=\"attachment-medium\" alt=\"dummy_img\" /></a></p>\n",
      "parent": null,
      "link": "http://tryitoneyewear.com/?attachment_id=28",
      "date": "2015-04-29T06:50:48",
      "modified": "2015-04-29T06:50:48",
      "format": "standard",
      "slug": "dummy_img",
      "guid": "http://tryitoneyewear.com//wp-content/uploads/2015/04/dummy_img.jpg",
      "excerpt": null,
      "menu_order": 0,
      "comment_status": "open",
      "ping_status": "open",
      "sticky": false,
      "date_tz": "UTC",
      "date_gmt": "2015-04-29T06:50:48",
      "modified_tz": "UTC",
      "modified_gmt": "2015-04-29T06:50:48",
      "meta": {
        "links": {
          "self": "http://tryitoneyewear.com/wp-json/media/28",
          "author": "http://tryitoneyewear.com/wp-json/users/1",
          "collection": "http://tryitoneyewear.com/wp-json/media",
          "replies": "http://tryitoneyewear.com/wp-json/media/28/comments",
          "version-history": "http://tryitoneyewear.com/wp-json/media/28/revisions"
        }
      },
      "terms": [],
      "source": "http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img.jpg",
      "is_image": true,
      "attachment_meta": {
        "width": 200,
        "height": 200,
        "file": "2015/04/dummy_img.jpg",
        "sizes": {
          "thumbnail": {
            "file": "dummy_img-150x150.jpg",
            "width": 150,
            "height": 150,
            "mime-type": "image/jpeg",
            "url": "http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img-150x150.jpg"
          },
          "owl_widget": {
            "file": "dummy_img-180x100.jpg",
            "width": 180,
            "height": 100,
            "mime-type": "image/jpeg",
            "url": "http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img-180x100.jpg"
          }
        },
        "image_meta": {
          "aperture": 0,
          "credit": "",
          "camera": "",
          "caption": "",
          "created_timestamp": 0,
          "copyright": "",
          "focal_length": 0,
          "iso": 0,
          "shutter_speed": 0,
          "title": "",
          "orientation": 0
        }
      }
    },
    "terms": []
  }
]

2 个答案:

答案 0 :(得分:0)

using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Security.Authentication; using System.Threading.Tasks; using System.Web; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Newtonsoft.Json; namespace TestConsole { internal class Program { private const string _commonAuthority = "https://login.microsoftonline.com/common/"; private static void Main(string[] args) { ConsoleKeyInfo kinfo = Console.ReadKey(true); AuthenticationContext ac = new AuthenticationContext(_commonAuthority); while (kinfo.Key != ConsoleKey.Escape) { if (kinfo.Key == ConsoleKey.A) { AuthenticationResult ar = ac.AcquireToken("https://babtecportal.onmicrosoft.com/Portal2015.Api", "client_id", new Uri("https://out.es"), PromptBehavior.Auto, UserIdentifier.AnyUser, "prompt=admin_consent"); } else if (kinfo.Key == ConsoleKey.C) { Console.WriteLine("Token cache length: {0}.", ac.TokenCache.Count); } else if (kinfo.Key == ConsoleKey.L) { ac.TokenCache.Clear(); HttpClient client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Get, _commonAuthority + "oauth2/logout?post_logout_redirect_uri=" + HttpUtility.UrlEncode("https://out.es")); var response=client.SendAsync(request).Result; Console.WriteLine(response.StatusCode); ac=new AuthenticationContext(_commonAuthority); } else { int num; if (int.TryParse(Console.ReadLine(), out num)) { try { AuthenticationResult ar = ac.AcquireToken("https://babtecportal.onmicrosoft.com/Portal2015.Api", "client_id", new Uri("http://out.es"),PromptBehavior.Auto,UserIdentifier.AnyUser); ac = new AuthenticationContext(ac.TokenCache.ReadItems().First().Authority); // Call Web API string authHeader = ar.CreateAuthorizationHeader(); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("http://localhost:62607/api/Values?num={0}", num)); request.Headers.TryAddWithoutValidation("Authorization", authHeader); HttpResponseMessage response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { string responseString = response.Content.ReadAsStringAsync().Result; Values vals = JsonConvert.DeserializeObject<Values>(responseString); Console.WriteLine("Username: {0}", vals.Username); Console.WriteLine("Name: {0}", vals.FullName); vals.Range.ToList().ForEach(Console.WriteLine); } else { Console.WriteLine("Status code: {0}", response.StatusCode); Console.WriteLine("Reason: {0}", response.ReasonPhrase); } } catch (AdalException ex) { Console.WriteLine(ex.Message); } } } kinfo = Console.ReadKey(true); } } } public class Values { public string Username { get; set; } public string FullName { get; set; } public IEnumerable<int> Range { get; set; } } } 不是数组,因此没有forEach函数可用。 要使用Object,请执行以下操作:

feature_image

答案 1 :(得分:0)

你应该学习更多关于json和javascript的信息

你的json是包含单个对象的数组。 featured_image也是对象,所以要获得你需要的值,比如

your_json[0].featured_image.guid