如何将解码数据从json存储到sqlite

时间:2015-03-20 03:16:40

标签: javascript json sqlite cordova phonegap-pushplugin

我想将来自我网站的json数据解码到应用程序。 该应用程序是使用phonegap构建的。 该消息通过GCM进入应用程序,但显示“未定义”。 我不知道解码消息。

这是js中的函数:

                   function onNotification(e) 
                   {
                    case 'message':

                    if (e.foreground)
                    {
                        $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
                    }
                    else
                    {   
                        if (e.coldstart)
                            $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
                        else
                        $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
                    }

                    $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');


                    //android only
                    $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
                    //amazon-fireos only
                    $("#app-status-ul").append('<li>MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp + '</li>');
                break;

                case 'error':
                    $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
                break;

                default:
                    $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
                break;
                   }
                  }

这里e.payload.message是要解码并存储到SQLite数据库中的项目。

当我使用stringify时,我得到了解码的数据。这是解码数据

这是我得到的回应。

MESSAGE -> MSG DECODED: {"payload":{"0":{"medicine_name":null},"1":{"tm_1":null},"2":{"tm_2":null},"3":{"tm_3":null},"4":{"dosage":null},"5":{"medicine_name":null},"6":{"tm_1":null},"7":{"tm_2":null},"8":{"tm_3":null},"9":{"dosage":null},"10":{"medicine_name":null},"11":{"tm_1":null},"12":{"tm_2":null},"13":{"tm_3":null},"14":{"dosage":null},"15":{"medicine_name":null},"16":{"tm_1":null},"17":{"tm_2":null},"18":{"tm_3":null},"19":{"dosage":null},"20":{"medicine_name":null},"21":{"tm_1":null},"22":{"tm_2":null},"23":{"tm_3":null},"24":{"dosage":null},"25":{"medicine_name":null},"26":{"tm_1":null},"27":{"tm_2":null},"28":{"tm_3":null},"29":{"dosage":null},"30":{"medicine_name":null},"31":{"tm_1":null},"32":{"tm_2":null},"33":{"tm_3":null},"34":{"dosage":null},"35":{"diagnosis":null},"36":{"instructions":null}}

现在我想知道如何使用sqlite将其存储在db中。?

谢谢。

1 个答案:

答案 0 :(得分:0)

以下可以在所有JavaScript应用程序中使用,甚至是Ionic / PhoneGap / Cordova。

考虑以下JSON:

[  
   {  
      "message":"How is it going",
      "fromUser":"akbaralam",
      "dateAdded":"Tue May 02 2017"
   },
   {  
      "message":"It is been so long that you saw me",
      "fromUser":"akbaralam",
      "dateAdded":"Tue May 02 2017"
   }
]

当使用 JSON.stringify 将此JSON转换为字符串时,我们得到:

"[{"message":"How is it going","fromUser":"akbaralam","dateAdded":"Tue May 02 2017"},{"message":"It is been so long that you saw me","fromUser":"akbaralam","dateAdded":"Tue May 02 2017"}]"

从字符串中获取对象我们执行 JSON.parse(yourStringifiedJson),我们将返回JSON对象。

希望这会有所帮助。