私はlocalstorageに新しいです.Jsonのデータを1つのファイルに格納し、他のfile.Belowのデータを取得しようとしています。 localstorageを使用してフィードデータを格納しようとしました。他のhtmlファイルでデータを取得するようになっています。しかし、フィードから最終オブジェクトのみを取得しています。他のファイルにすべてのフィードオブジェクトを取得できます。localstorage fronの1つのファイルを使用してオブジェクトの配列を他のファイルに保存する方法
{
"channel":{
"id":9,
"name":"my_house",
"description":"Netduino Plus connected to sensors around the house",
"latitude":"40.44",
"longitude":"-79.9965",
"field1":"Light",
"field2":"Outside Temperature",
"created_at":"2010-12-14T01:20:06Z",
"updated_at":"2017-02-13T09:09:31Z",
"last_entry_id":11664376
},
"feeds":[{
"created_at":"2017-02-13T09:07:16Z",
"entry_id":11664367,
"field1":"196",
"field2":"31.507430997876856"
},{
"created_at":"2017-02-13T09:07:31Z",
"entry_id":11664368,
"field1":"192",
"field2":"30.743099787685775"
},{
"created_at":"2017-02-13T09:07:46Z",
"entry_id":11664369,
"field1":"208",
"field2":"28.280254777070063"
}]}
One.htmlここで私は)
$.ajax({
url : "https://api.thingspeak.com/channels/9/feeds.json?results=3",
dataType:"json",
cache: false,
error:function (xhr, ajaxOptions, thrownError){
debugger;
alert(xhr.statusText);
alert(thrownError);
},
success : function(json1) {
console.log(json1);
json1.feeds.forEach(function(feed, i) {
console.log("\n The deails of " + i + "th Object are : \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2);
localStorage.setItem('Created_at', feed.created_at);
var create = localStorage.getItem('Created_at');
console.log(create);
localStorage.setItem('Entry_id', feed.entry_id);
var entry = localStorage.getItem('Entry_id');
console.log(entry);
localStorage.setItem('Field1', feed.field1);
var fd1 = localStorage.getItem('Field1');
console.log(fd1);
localStorage.setItem('Field2', feed.field2);
var fd2 = localStorage.getItem('Field2');
console.log(fd2);
});
other.htmlをすべてのフィードのデータを格納しています:-(:(ここで私は
<script>
// Called on body's `onload` event
function init() {
// Retrieving the text input's value which was stored into localStorage
var create = localStorage.getItem('Created_at');
console.log(create);
document.writeln("<br>Created_at = "+create);
var entry = localStorage.getItem('Entry_id');
document.writeln("<br>Entry_id = "+entry);
var fd1 = localStorage.getItem('Field1');
document.writeln("<br>Field1 = "+fd1);
var fd2 = localStorage.getItem('Field2');
document.writeln("<br>Field2 = "+fd2);
}
</script>
私が試してみましたが、再び私はちょうど【選択コードがこれを下回る私がしようとしているときだけ1オブジェクトを取得していますあなたが私が間違っているところで私を助けてもらえますか?敬具@ Vivek Pradhan。 New.html:var feedsArray = []; feedsArrayプッシュ(フィード); \t \t \t \t \t \t \t \t \t \t localStorage.setItem( 'feedsArray'、JSON.stringify(feedsArray))。 new2.html:var feedsArray = []; var create = JSON.parse(localStorage.getItem( 'feedsArray')); console.log(作成); @Anusha。 – Anusha
再び 'feedsArray'を作成し、' var feedsArray = [];を設定します。 'for'ループの内側にあります。それが理由です。それは動作しません。 forループの外側に配列を定義し、それをlocalStorageに設定する必要があります。その後、 'for'ループの中で' var savedArr = JSON.parse(localStorage.getItem( 'feedsArray')); 'を実行する必要があります。その後、フィードアイテムを 'savedArr'配列にプッシュし、ローカルストアの –
Tqで再度更新して、問題を解決しました。@ Vivek Pradhan – Anusha