0
我想從一個url中獲取JSON數據並存儲在sqlite中db.from sqlite數據庫我將數據轉換爲JSON數組對象並使用該值我tryinf執行if else條件我有沒有對象在JSON created_at和field3是要執行的數據,如果field3是0我不得不忽略,如果field3是1,我們必須計算日期n時差從該對象日期n時間下一個obj,其中field3以1結束。我的代碼如下。如果其他條件解析錯誤
function renderList1(tx,result){
newJson = [ ];
if (result != null && result.rows != null) {
for (var k = 0; k < result.rows.length; k++) {
var row = result.rows.item(k);
newJson[k] = { created_at:row.Created_at,entry_id:row.entry_id,field1:row.Field1,field2:row.Field2,field3:row.Field3 };
}
}
var jsonAll = JSON.stringify(newJson);
console.log(jsonAll);
var feeds = $.parseJSON(jsonAll);
console.log(feeds);
power();
function power(){
for (var i=0;i<feeds.length;i++) //feeds contain the JSON data
{
if(feeds[i]["field3"] === "1"){
//Start Time
var d1 = new Date(feeds[i]["created_at"]);
console.log(d1);
for (var j=i+1;j<feeds.length;j++) //compare objects starting from 'i' till next "1" is found
{
if(feeds[j]["field3"] === "0")
{
//End Time
var d2 = new Date(feeds[j]["created_at"]);
console.log(d2);
j=feeds.length;
}
}
//Time difference in milli seconds
var diff = d2.getTime() - d1.getTime();
var diffInHours= diff/1000diff/ 3600/1000;
console.log(diffInHours);
var pow = Powercon * diffInHours;
console.log(pow);
}else{
console.log("Field3 is 0");
}
}
}
JSON數據:
[
{
"created_at": "2017-04-12T08:53:46Z",
"entry_id": 37368,
"field1": "14",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:03:54Z",
"entry_id": 37369,
"field1": "14",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:14:02Z",
"entry_id": 37370,
"field1": "14",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:24:03Z",
"entry_id": 37371,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:34:06Z",
"entry_id": 37372,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:44:09Z",
"entry_id": 37373,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:54:12Z",
"entry_id": 37374,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:04:20Z",
"entry_id": 37375,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:24:20Z",
"entry_id": 37376,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:44:27Z",
"entry_id": 37377,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:54:30Z",
"entry_id": 37378,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:04:34Z",
"entry_id": 37379,
"field1": "16",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:14:37Z",
"entry_id": 37380,
"field1": "16",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:24:40Z",
"entry_id": 37381,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:34:43Z",
"entry_id": 37382,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:44:46Z",
"entry_id": 37383,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:54:49Z",
"entry_id": 37384,
"field1": "16",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T12:04:53Z",
"entry_id": 37385,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T12:14:56Z",
"entry_id": 37386,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T12:25:00Z",
"entry_id": 37387,
"field1": "16",
"field2": "0",
"field3": "0"
}
]
我想上面的代碼,當我運行我的,如果和其他條件,如果場3等於1了一個問題,如果條件,但它要它應該執行其他條件。任何人都可以幫助我哪裏出錯?謝謝
你嘗試過嗎?(feeds [i] [「field3」] === 1){?可能是它的整數。 – abhiklpm
@Anusha嘗試'如果(飼料[我] [「field3」] ==「1」)' – user3441151
它的工作[email protected] – Anusha