JavaScript的$ http.get一个JSON数组并将其存储在localStorage的 [英] JavaScript $http.get an json array and store it in localstorage

查看:269
本文介绍了JavaScript的$ http.get一个JSON数组并将其存储在localStorage的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想接收JSON数组并将其存储在本地存储。
我见过这样的一些问题,但他们不能帮助我。所以我想尝试用我自己的问题。即时通讯发展的框架离子

I'm trying to receive an json Array and store it in the local storage. I've seen some questions like this but they couldn't help me. So i wanted to try with my own question. Im developing in Ionic Framework.

我的控制器包括以下内容:

My controller includes following:

app.controller('QuestionsCtrl', function($scope, $http, $stateParams, $localstorage) {

$scope.getData = function() {

// get the json
   $http.get("data/fragenArray.json")
        .success(function(data) {

        //output of json as a string  -> correct
        console.log('data: ' + JSON.stringify(data));

        // store json in local storage
        $localstorage.setObject('fragenset', data);

        // restore json from local storage
        var post = $localstorage.getObject('fragenset');

        // output of local storage item -> incorrect
        // I got: Test xxx: [object Object]
        console.log('Test xxx: ' + post);
     })
     .error(function(data) {
            alert("ERROR");
        });
    }
 });

要保存我已经得到了JSON:

To store the json I've got:

angular.module('utils', [])

 .factory('$localstorage', ['$window', function($window) {
   return {
     set: function(key, value) {
       $window.localStorage[key] = value;
     },
     get: function(key, defaultValue) {
       return $window.localStorage[key] || defaultValue;
     },
     setObject: function(key, value) {
       $window.localStorage[key] = JSON.stringify(value);
     },
     getObject: function(key) {
       return JSON.parse($window.localStorage[key] || '{}');
     }
   }
 }]);

所以我决定尝试没有ht​​tp.get要求:

So I decided to try it without the http.get request:

app.run(function($localstorage, $http) {
   $localstorage.setObject('post', {"fragen":[
    {
        "id":"1",
        "frage":"Wie ist das Wetter?",
        "antworten": {
            "a_1":"gut",
            "a_2":"schlecht"
        }
    },
    {
        "id":"2",
        "frage":"Wie geht es dir?",
        "antworten": {
            "a_1":"gut",
            "a_2":"schlecht"
        }
    }
 ]});
   var post = $localstorage.getObject('post');
   console.log(post);
 })

和结果IST正是我所期待的 - 一个JSON对象

And the result ist exactly what i expected - an json object.

那么,怎样才能我JSON数组正确存储从http.get?

So how can i store the json array from the http.get correctly?

推荐答案

我们知道,因为它通过与写的测试数据检索工作正常JSON.stringify()

We know your data retrieval is working correctly because it passes the test written with JSON.stringify():

// get the json
   $http.get("data/fragenArray.json")
        .success(function(data) {

        //output of json as a string  -> correct
        console.log('data: ' + JSON.stringify(data));

        // store json in local storage
        $localstorage.setObject('fragenset', data);

        // restore json from local storage
        var post = $localstorage.getObject('fragenset');

        // output of local storage item -> incorrect
        // I got: Test xxx: [object Object]
        console.log('Test xxx: ' + post);
     })

最后一次测试是一个糟糕的考验。这并不是说是不正确的结果。测试的编码不正确。

The last test is a bad test. It is not the result that is incorrect. The coding of the test is incorrect.

这是因为你不能将其追加为一个字符串检查的对象。

This is because you can not inspect an object by appending it to a string.

如果X是一个对象,那么无论什么十载,
也许 X = {'A​​':123}

If X is an object, then no matter what X contains, perhaps X = {'a': 123}

console.log("anything "+X); 
// --> "anything [object Object]"

这是因为东西+ X 字符串前pression 的,当一个对象被强制转换为字符串,使用Javascript替换与字符串对象的翻译:

This is because "anything "+X is a string expression and when an object is coerced to string, Javascript replaces the object with the string "[object Object]"

下面是你应该考什么,而不是:

Here is what you should test instead:

 //output of storage as a json string  
 console.log('post: ' + JSON.stringify(post));

最后

 // json string of storage matches json string of input data
 console.log(JSON.stringify(data)===JSON.stringify(post));
 // will yield true or false

这篇关于JavaScript的$ http.get一个JSON数组并将其存储在localStorage的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆