0

I have create web services using Web Api in mvc3,in this i want get the data from json. Json Result like this

   {"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":""}
    ]
  }}

for this i have create Poco class and i get Order data using poco class, but i can't get the items array data how can i get items data

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;


        }

Please help me..

1 Answer 1

1

Try this (you need to fix your Json by replacing the "=" signs by ":" signs" before):

[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);
}
8
  • Hi thank you for giving response i have tested this using fiddler in that header i have added content-type and accept headers to "application/json" it showing 500 internal error. Commented Nov 14, 2011 at 10:01
  • My question is how can i get the nested array data to store in my db with same order id. Commented Nov 14, 2011 at 10:03
  • when i run the fiddler it shows 500 Internal error.But when pass the json like " {"LocationId":5,"DeviceIdentifier":"N","OrderContactName":"Victor","OrderContactPhone":"1234569874","OrderContactEmail":"info@ses","ShipMethod":"PickUp","PickupDate":"2011-09-08","Amount":"15.25","MenuItemId":"1_5_9","Quantity":"2_9_8","UnitCost":"2.25_6.65_9.95","Options":"MakeSpicyRaost"} " It is working .. Commented Nov 14, 2011 at 10:17
  • i have fallow this link wcf.codeplex.com/… To create Webservices. Commented Nov 14, 2011 at 10:19
  • Hi i get the values of order but how can i get Item values which is having the nested array values .. Commented Nov 14, 2011 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.