1

Guys I'm stuck with something. I have to convert a Json string to an object list. I am working on a MVC project and I'm in the middle of an API integration.

Here's the data of the problem. I managed to get data list(has a tree structure) object from a cloud api and converted it to a Json string in MY WEBAPI.

This is the query

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

This is the converting part and I returned it as a string to the webUI.

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

I need to convert this again to a object tree in the angular controller of my webUI. I tried angular.fromJson but it doesn't work

 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)

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

I think this will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly!! Thanks alot! :D

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.