JSON对象和Javascript对象 [英] JSON objects and Javascript Objects

查看:53
本文介绍了JSON对象和Javascript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Web应用程序的前端。我收到JSON中的所有响应,解析它并将其作为html。我看了几个javascript MVC框架(即backbone.js,ember.js等),发现它们对我的简单应用程序来说太过分了。但我发现真正令人着迷的是他们都有模特。 我发现我可以创建javascript对象,它们将类似于模型,因为我没有这么复杂的需求,我的自定义javascript对象很容易制作。

I am creating front-end of a web app. I am receiving all the responses in JSON, parsing it and putting it as html. I had a look at few javascript MVC frameworks (i.e backbone.js, ember.js etc) and found that they were just overkill for my simple app. But what I found really fascinating is that they all had Models. I figured out I can create javascript objects and they will be similar to models and as I dont have such complex needs my custom javascript objects shall be easy to make.

function displayCar() {
  var result = "A Beautiful " + this.year + " " + this.make
    + " " + this.model;
  pretty_print(result);
}

function Car(make, model, year, owner) {
  this.make = make;
  this.model = model;
  this.year = year;
  this.owner = owner;
  this.displayCar = displayCar;
}

我从服务器接收汽车作为JSON响应。 那么理想的方法是什么:在循环JSON数组时创建对象或者我应该只使用解析并直接从那里显示html?

I receive the cars as JSON response from the server. SO what would be the ideal way : To create the objects while looping over the JSON array OR should I just use the parsing and directly show html from there?

现在我的问题是我应该将JSON解析为javascript对象吗?我也不确定使用这种对象会有多大帮助吗? 我还想知道是否有一种直接从JSON创建自定义预定义对象的方法,就像我觉得的那样可能有用,比如我想要车对象

推荐答案

我做了一些回答来回答你的问题。如果有任何问题请告诉我,我会根据情况编辑问题。

I make some assumtions to answer your questions. Please tell me if any is wrong and i will edit the question accordingliy.


我从服务器接收汽车作为JSON响应。那么理想的方法是:在循环JSON数组时创建对象或者我应该只使用解析并直接从那里显示html?

I receive the cars as JSON response from the server. SO what would be the ideal way : To create the objects while looping over the JSON array OR should I just use the parsing and directly show html from there?



答案



如果你想为数组中的每个对象添加一些方法,你需要那些预定义的类。

Answer

If you want to add some methods to each object on the array, you need those predefined "class".


我应该将JSON解析为javascript对象吗?

should I parse the JSON into javascript objects?



答案



您必须将JSON解析为javascript对象才能使用数据。

Answer

You will have to parse the JSON into a javascript Object just to use the data.


使用这种对象会有多大帮助?

Using this kind of objects would be much helpful?



答案



如果您要引用Javascript对象,它们确实非常实用且灵活。您可以在解析后直接使用Javascript对象中JSON上定义的属性。

Answer

If you are refering to Javascript objects, they are really useful and flexible. You can directly use the properties defined on the JSON from the Javascript object after it's parsed.

使用从JSON获取的对象数组,如下所示:

Suposse an array of Objects taken from JSON, as follows:

JSON_ARRAY = [
  {
    "make": "ACME inc",
    "model": "road runner 9999",
    "year": "the future",
    "owner": "nobody"
  },
  // more cars here...
]

您可以通过 JSON_ARRAY [n] ,其中 n 是数组中汽车的索引。

you could access each car through JSON_ARRAY[n], where n is the index of the car in the array.

您还可以添加 displayCar JSON_ARRAY上每个对象的方法

for (var i = 0; i < JSON_ARRAY.length; i++) {
    JSON_ARRAY.displayCar = displayCar;
}



问题



Question


我还想知道是否有一种直接从JSON创建自定义预定义对象的方法,因为我觉得它可能像我想要的汽车对象一样有用。

I would also like to know if there is a way to create custom pre-defined objects straight from JSON, as I feel than it might be useful like I want to car objects.



回答



不,没有办法预先定义类 JSON。如果需要,您可以定义一个旨在适应自身JSON表示的类。

Answer

No, there isn't a way to pre-define a "class" from JSON. You can define a class designed to adapt to a JSON representation of itself, if you want.

function Car( car_json ) {
  this.make = car_json.make;
  this.model = car_json.model;
  this.year = car_json.year;
  this.owner = car_json.owner;
}

Car.prototype.displayCar = function() {
   // stuff
}

甚至更好:

function Car( json_data ) {
  for( var property in json_data ) {
      this[ property ] = json_data[ property ];
  }
}

无论如何,你必须映射对象数组到一系列汽车:

Anyway, you will have to map the array of objects to an array of Cars:

var cars = JSON_CARS_ARRAY.map(function jsonToCar( json ) {
  return new Car( json );
});

这篇关于JSON对象和Javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆