2012-11-08 154 views
0

我想設計一個JSON對象,將與澤西島和傑克遜工作。JSON設計與查詢澤西島

對JSON/Restful編程來說,這是相當新的,所以我想知道下面是否可行。

{ 
"name": "myservice", 

"orders": [ 
    { 
     "name": "iphone", 
     "description": "iPhone 5", 
     "providers": [ 
      { 
       "name": "a", 
       "description": "AT&T", 
       "pricing": ["$40", "$70", "$120"] 
      }, 
      { 
       "name": "b", 
       "description": "Verizon", 
       "pricing": ["$45", "$60", "$85"] 
      } 
     ] 
    }, 
    { 
     "name": "galaxy3", 
     "description": "Samsung Galaxy 3", 
     "providers": [ 
      { 
       "name": "a", 
       "description": "AT&T", 
       "pricing": ["$45", "$60", "$85"] 

      } 
     ] 
    } 
] 
} 

獲得有關iPhone的Verizon公司提供的所有信息:

curl GET -H'Content-Type: application/json' https://mydomain/myservice/iphone/b 

將返回:對iPhone的定價

{ 
    "name": "b", 
    "description": "Verizon", 
    "pricing": ["$45", "$60", "$85"] 
} 

獲取列表AT &牛逼提供商:

curl GET -H'Content-Type: application/json' https://mydomain/myservice/iphone/a?pricing 

會返回:

{ 

    ["$40", "$70", "$120"] 

} 

任何示例或反饋將不勝感激!

+0

您的問題是什麼?什麼不行? – 2012-11-08 21:32:19

回答

0

下面是有關定義REST API商量好了:REST Complex/Composite/Nested Resources

這裏是什麼,我會在你的JSON改變: 1.訂單 - >順序,因爲資源被聲明爲單數名詞 2.供應商 - >供應商,因爲同樣

這是我怎麼會從客戶端調用,如果我知道我需要獲得(使用複合資源):

  1. https://<mydomain>/myservice/order/iphone/provider/b
  2. https://<mydomain>/myservice/order/iphone/provider/a/pricing

如果你需要搜索的順序,你可以這樣定義的要求:

  1. https://<mydomain>/myservice/order?name=iphone - >,將在「命令」列表中返回的第一個元素

假設是「名稱」是各自資源的關鍵(訂單和提供商)