2017-12-26 1 views
1

Jungs Ich bin mit etwas fest. Ich muss eine JSON-Zeichenfolge in eine Objektliste konvertieren. Ich arbeite an einem MVC-Projekt und bin mitten in einer API-Integration.Convert Json String zu Objekt Liste in Winkel Controller MVC Web-Anwendung

Hier sind die Daten des Problems. Ich habe es geschafft, ein Datenlistenobjekt (hat eine Baumstruktur) von einer Cloud-API zu erhalten und es in eine JSON-Zeichenfolge in MY WEBAPI konvertiert.

Dies ist die Abfrage

  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, 
        }); 

Dies ist die Umwandlung Teil und ich kehrte er als String an der Web-Oberfläche.

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

Ich muss dies wieder in einen Objektbaum in der Winkelsteuerung meiner WebUI konvertieren. Ich versuchte angular.fromJson, aber es funktioniert nicht

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)

Antwort

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); 
} 

Ich denke, das wird funktionieren.

+0

Funktioniert perfekt !! Danke vielmals! : D – isuru