【问题标题】:How to Get the Array of data from json array using WEB API in mvc3?如何在 mvc3 中使用 WEB API 从 json 数组中获取数据数组?
【发布时间】:2011-12-28 12:26:03
【问题描述】:

我在 mvc3 中使用 Web Api 创建了 Web 服务,我想从 json 中获取数据。像这样的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": "Info@mail.com",
   "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 类并使用 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;


        }

请帮帮我..

【问题讨论】:

    标签: asp.net-mvc-3 wcf-web-api


    【解决方案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);
    }
    

    【讨论】:

    • 您好,感谢您的回复我已经在该标头中使用提琴手对此进行了测试,我已添加内容类型并接受标头到“应用程序/json”,它显示 500 内部错误。
    • 我的问题是如何让嵌套数组数据以相同的订单 ID 存储在我的数据库中。
    • 当我运行提琴手它显示 500 内部错误。但是当传递 json 像“ {“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"} " 它正在工作..
    • 我已经闲置了这个链接wcf.codeplex.com/… 来创建 Web 服务。
    • 嗨,我得到了 order 的值,但是我怎样才能得到具有嵌套数组值的 Item 值..
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2020-08-18
    相关资源
    最近更新 更多