0

I have a multi-platform Cordova app backed by a .net web service which returns JSON data for syncing. This works fine.

I am now trying to add native calls into the Android version of the app which are hooked into BroadcastReceiver so that I can provide rich notifications when the app isn't running.

The scheduling and execution of these events are running fine; but I am having real problems in processing the result of the web service call.

The web service call (VB.net) is:

<OperationContract()>
<WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json)>
Public Function SyncNotifications_Native(ByVal dateAfter As String, ByVal which As Integer, ByVal userID As Int32) As String

Dim rowsList As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object)
:::
:::
While dr.Read()
                row = New Dictionary(Of String, Object)
                row.Add("allow_reply", dr("allow_reply"))
                row.Add("content", dr("content"))
                row.Add("date_added", dr("date_added")
                row.Add("date_deleted", dr("date_deleted")
                row.Add("date_read", dr("date_read")
                row.Add("deleted", dr("deleted"))
                row.Add("last_update", Now().ToString("yyyy-MM-dd HH:mm:ss"))
                row.Add("notification_id", dr("notification_id"))
                row.Add("subject_line", dr("subject_line"))
                row.Add("user_id", dr("user_id"))
                rowsList.Add(row)
            End While

Return New JavaScriptSerializer().Serialize(details)
End Function

This should return an array of zero or more entries. I'm using an AsyncTask to get the data. This is the data I get back from the call:

{"d":"
[{\"allow_reply\":1,\"content\":\"test content\",
\"date_added\":\"2016-02-04 23:37:50\",\"date_deleted\":\"\",
\"date_read\":\"\",\"deleted\":0,\"last_update\":\"2016-02-04 23:38:43\",
\"notification_id\":27,\"subject_line\":\"test\",\"user_id\":1}]
"}

I've looked at various resources and have tried all types of solutions. This is what I'm currently doing:

Convert the return string to a JSON Object:

JSONObject json2 = new JSONObject(s);

Try to get the array out of the object:

JSONArray results = json2.getJSONArray("d");

This way gives me an error of:

java.lang.String cannot be converted to JSONArray

I've seen a suggestion in another post that I'm actually returning a SOAP response from my web service; I'm not sure, I haven't seen any other example JSON responses which start with

{"d":

How can I properly get the returned array into a JSONArray so that I can pass it off to another function?

Thanks

DaveSav
  • 1,364
  • 5
  • 21
  • 41
  • The error: java.lang.String cannot be converted to JSONArray is because the value of "d": is a String. Look: {"d": "[..]". You should return an array like this format: {"d": [..] without the quotes – JpCrow Feb 05 '16 at 00:45
  • That's how the web service sends it. I've added more details to the web service code if that is of any help? – DaveSav Feb 05 '16 at 00:56
  • I don't know how you can fix this on server side, but on client you can use `JSONObject obj = new JSONObject(s); String arrStr = obj.getString("d"); JSONArray array = new JSONArray(arrStr);` – varren Feb 05 '16 at 01:14
  • Your code is what I went with. – DaveSav Feb 05 '16 at 23:58

1 Answers1

1

Am not a Java dev so:

  • {"d": "[{\"allow_reply\":1,\....."} is an Object
  • with a d field
  • whose value is a string "[{\"allow_reply\":1,\....."} and is why you get java.lang.String cannot be converted to JSONArray
  • so you'll have to parse (aka "deserialize")

So in Javascript (you'll have to translate to Java):

//this is the data your service returns
var foo = {"d": "[{\"allow_reply\":1,\"content\":\"test content\",\"date_added\":\"2016-02-04 23:37:50\",\"date_deleted\":\"\",\"date_read\":\"\",  \"deleted\":0,\"last_update\":\"2016-02-04 23:38:43\",\"notification_id\":27,\"subject_line\":\"test\",\"user_id\":1}]"};

//parse the string
var data = JSON.parse(foo.d); 

data now contains your array

So in your code above, you have to parse the string and convert it into the object it represents (from a string).

Hth...

Community
  • 1
  • 1
EdSF
  • 11,753
  • 6
  • 42
  • 83