1

マップの境界を JSON オブジェクトに保存したいと思います。コード補完のために、JSDoc を使用してオブジェクトを宣言しました。

/**
 * @name MapBounds
 * @class MapBounds
 * This class represents a map boundary definition.
 *
 * @property {number} minLat Specifies the minimum latitude of the boundary.
 * @property {number} minLon Specifies the minimum longitude of the boundary.
 * @property {number} maxLat Specifies the maximum latitude of the boundary.
 * @property {number} maxLon Specifies the maximum longitude of the boundary.
 */

MapBounds オブジェクトを作成する段階になると、私のコードは次のようになります (この例には実際の緯度/経度の値はありません)。

var newMapBounds = MapBounds( {minLat: 50, minLon: 50, maxLat: 100, maxLon: 100} );

JSON を使用してオブジェクトを指定し、必要な型にキャストしています。これを行うより良い方法はありますか?

ありがとう。

4

2 に答える 2

1

その形式で Json オブジェクトを作成する方法は次のとおりです (これは単なる JavaScript オブジェクト リテラルです)。

{
 minLat: 0, // Specifies the minimum latitude of the boundary
 minLon: 0, // Specifies the minimum longitude of the boundary
 maxLat: 0, // Specifies the maximum latitude of the boundary
 maxLon: 0  // Specifies the maximum longitude of the boundary
}

Json オブジェクトを Web サービスに送信するときに、Json オブジェクトをクラスに "マップ" する必要があるのは非常に一般的です。一部のフレームワーク (MVC .NET など) は、JSON オブジェクトをリクエストに渡し、オブジェクト タイプを Web サービス パラメータとして指定すると、これを行います。それにも関わらず、上記の形式は、JavaScript 側でその構造を表現する方法です。

編集: JSON オブジェクトからサーバー側コードの指定されたクラスへのこのタイプのマッピングは、サーバーで使用しているフレームワーク (.NET / MVC) によって発生するか、サーバーで手動で行います。JSON オブジェクトをサーバーに送信すると、サーバー側のコードが引き継がれるため、クライアント側 (つまり JavaScript) でその側を文書化する必要はありません。

于 2013-01-25T20:15:13.970 に答える
0
于 2013-01-25T20:23:25.023 に答える