0

I want to decode json data coming from my website to the application. The app is built using phonegap. The message is coming to the app via GCM but it is showing "undefined". I have no idea about decoding the message.

Here is the function in 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;
                   }
                  }

Here e.payload.message is the item to be decoded and stored into SQLite database.

When I used stringify I got the data decoded. Here is the decoded data

This is the response I got.

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}}

Now I want to know How can I store it in db using sqlite.?

Thank you.

V.V
  • 107
  • 1
  • 1
  • 10

1 Answers1

0

Following can in all JavaScript applications, even Ionic/PhoneGap/Cordova.

Consider the following 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"
   }
]

when this JSON is converted to string using JSON.stringify we get:

"[{"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"}]"

to get the object back from string we do JSON.parse(yourStringifiedJson) and we will get back the JSON objects.

Hope this helped.

Abhay Shiro
  • 3,431
  • 2
  • 16
  • 26