2011-11-14 75 views
0

我已經在mvc3中使用Web Api創建了Web服務,在這裏我想從json獲取數據。 JSON結果這樣如何使用mvc3中的WEB API從json數組獲取數據數組?

{"order": { 
    "locationid": "1", 
    "deviceidentifier": "XXXXXXXXXXXXXXXXXXXXXXX", 
    "ordercontactname": "XXXXXXXXXXXXXXXXXXXXXXX", 
    "ordercontactphone": "XXXXXXXXXXXXXXXXXXXXXXX", 
    "ordercontactemail": "XXXXXXXXXXXXXXXXXXXXXXX", 
    "shipaddress1": "17 Brookfield", 
    "shipaddress2": "Suite 17", 
    "shipcity": "Irvine", 
    "shipstate": "CA", 
    "shipzipcode": "92604", 
    "shipphonenumber": "9493742114", 
    "shipemail": "[email protected]", 
    "shipmethod": "pickup", 
    "billingfirstname":"Tester", 
    "billinglastname":"test", 
    "billingmiddleinitial":"S", 
    "billingaddress":"field", 
    "billingcity":"Sample", 
    "billingstate":"SM", 
    "billingzipcode":"523201", 
    "billingphonenumber": "1234567891", 
    "billingemail": "", 
    "paytype":"creditcard", 
    "amount"="10.50", 
    "acctno"="123456789987", 
    "exproute"="0114", 
    "coupon"="X2323", 
    "notes"="", 
    "items": [ 
    {"itemid":"1","quantity":"1","price":"2.5","notes":"make it spicy"}, 
    {"itemid":"4","quantity":"2","price":"4.5","notes":""}, 
    {"itemid":"3","quantity":"1","price":"1.5","notes":""} 
    ] 
    }} 

爲了這個,我有創造波蘇類和我使用POCO類獲取訂單數據,但我不能讓項目陣列中的數據我怎麼能得到項目數據

Here is my code 

    public List<Message> PlaceOrder(PlaceOrder order) 
     { 
      // List<PlaceOrder> entities =   (List<PlaceOrder>)JavaScriptConvert.DeserializeObject(json, typeof(List<PlaceOrder>)); 
     int PayOrderID = 0; 
     List<Message> Result; 
     Result = new List<Message>(); 
     try 
     { 
      Order objOrder = new Order(); 
      PayHist objPayhis = new PayHist(); 
      objOrder.LocationId = order.LocationId; 
      objOrder.DeviceIdentifier = order.DeviceIdentifier; 
      objOrder.OrderContactName = order.OrderContactName; 
      objOrder.OrderContactPhone = order.OrderContactPhone; 
      objOrder.OrderContactEmail = order.OrderContactEmail; 
      string guid = Guid.NewGuid().ToString(); 
      objOrder.ShipMethod = order.ShipMethod; 
      objOrder.ShippingDate = Convert.ToDateTime(order.PickupDate); 
      objOrder.OrderGuid = guid; 
      entities.AddObject("Orders", objOrder); 
      entities.SaveChanges(); 
      int orderId = objOrder.OrderId; 
      PayOrderID = orderId; 
      objPayhis.OrderId = orderId; 
      objPayhis.PayType = order.ShipMethod; 
      objPayhis.Amount = float.Parse(order.Amount); 
      entities.AddObject("PayHists", objPayhis); 
      entities.SaveChanges(); 
      JavaScriptSerializer ser = new JavaScriptSerializer(); 
      // Order foo = ser.Deserialize<Order>(json); 

      Message objMessage = new Message(); 
      objMessage.Messagevalue = "Sucess"; 
      Result.Add(objMessage); 
      return Result; 


     } 

請幫我..

回答

1

試試這個(你需要通過更換「=」的招牌來修復您的JSON「:」標誌」前):

[WebInvoke(Method = "POST", UriTemplate = "")] 
public HttpResponseMessage Add(JsonValue json) { 
    JsonValue order = json["order"]; 
    JsonArray items = (JsonArray) order["items"]; 
    JsonValue item1 = items[0]; 
    var notes1 = item1["notes"]; 
    return new HttpResponseMessage(HttpStatusCode.OK); 
} 
+0

喜謝謝你給我的迴應,我已經測試了這個使用提琴手在該頭我已經添加內容類型和接受頭到「應用程序/ JSON」它顯示500內部錯誤。 – Victor

+0

我的問題是我怎樣才能得到嵌套的數組數據存儲在我的數據庫相同的訂單號。 – Victor

+0

當我運行的提琴手它顯示500內部error.But當通過像「{」LocationId「:5,」DeviceIdentifier「:」N「,」OrderContactName「:」維克多「,」OrderContactPhone「:」1234569874「, 「OrderContactEmail」: 「信息@ SES」, 「ShipMethod」: 「代答」, 「PickupDate」: 「2011-09-08」, 「金額」: 「15.25」, 「MenuItemId」: 「1_5_9」, 「數量」: 「2_9_8」,「UnitCost」:「2.25_6.65_9.95」,「選項」:「_ MakeSpicyRaost_」}「這是工作.. – Victor

相關問題