在角度控制器MVC Web应用程序中将Json字符串转换为对象List

问题描述 投票:1回答:1

伙计们,我坚持不懈。我必须将Json字符串转换为对象列表。我正在开发一个MVC项目,我正在进行API集成。

这是问题的数据。我设法从云API获取数据列表(具有树结构)对象,并将其转换为MY WEBAPI中的Json字符串。

这是查询

            var textvar = (from avbH in avb_Hotels
                   join catRs in cat_Rs on avbH.categoryCode equals catRs.code
                   join rep in hotelRepList on avbH.code equals rep.code

                select new
                 {
                   code= avbH.code,
                   destinationCode = avbH.destinationCode,   
                   description = rep.description,                                                      
                   hotelstar = catRs.simpleCode,
                   checkIn = hotelBooking.DepartureDate,
                   checkOut = hotelBooking.ReturnDate,
                   name = avbH.name,
                   address = rep.address,
                   accommodationTypeCode = rep.accommodationTypeCode,
                   minRate = (int)Math.Round(Convert.ToDecimal(avbH.minRate) * rates),
                   images = "http://photos.hotelbeds.com/giata/" + rep.imagepath,
                   rooms = avbH.rooms,
                   ratecomment = avbH.ratecomment,
                  });

这是转换部分,我将其作为字符串返回给webUI。

     result = JsonConvert.SerializeObject(textvar2, Newtonsoft.Json.Formatting.None);// then returns result

我需要将它再次转换为我的webUI的角度控制器中的对象树。我试过angular.fromJson,但它不起作用

 app.service("APIService", function($http) {
this.hotelavailability = function(sub) {
    return $http({
        method: "post",
        data: sub,
        contentType: "application/json; charset=utf-8;text/plain",
        timeout:30000,
        url: "/api/HotelBooking/Availability"

    });
}

app.controller("APIController", function ($scope, $window, $http, filterFilter, APIService, States) {


    var getAll = "";
    getAll = APIService.hotelavailability(sub);


    getAll.then(function (d) { // d has the returning Json string
        console.log("Succss");
        $scope.hotels = angular.fromJson(d.data); //deserialization<-- This doesnt work         
        console.log($scope.hotels);
        $scope.respData = angular.fromJson(d.data);
      }

This is d(returning Json string from the webAPI)

angularjs json asp.net-web-api deserialization
1个回答
1
投票
getAll.then(function (d) { // d has the returning Json string
    console.log("Succss");
    $scope.hotels = angular.fromJson(d.data);         
    $scope.hotellist = angular.fromJson($scope.hotels); 
}

我认为这会奏效。

© www.soinside.com 2019 - 2024. All rights reserved.