伙计们,我坚持不懈。我必须将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);
}
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);
}
我认为这会奏效。