Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have written a C# web service as below to generate a JSON string or so I assumed.

List<Dictionary<String, Object>> lstdict = new List<Dictionary<String, Object>>();

...
...
... Logic for connecting db and getting records in msqldat (data reader) goes here.
    while (msqldat.Read())
    {
         var detls = new Dictionary<string, object>();
         for (int i = 0; i < msqldat.FieldCount; i++)
             {
                 detls.Add(msqldat.GetName(i), msqldat.IsDBNull(i) ? null : 
                 msqldat.GetValue(i));
                 lstdict.Add(detls);
            }
    }
JavaScriptSerializer jss = new JavaScriptSerializer();
String mret = jss.Serialize(lstdict);



The above webservice method is called in my java code and the below string is received.

{"GetDataResult":"[
{\"uname\":\"hkIUZIikXVTC5aNaSva8IQ==\",\"passwd\":\"hkIUZIikXVTC5aNaSva8IQ==\",\"validupto\":\"\\\/Date(1545330600000)\\\/\",\"dept\":\"juubHSHgLr\/3JWnrZCh5LeeW5Q7lioWOZ1\/Tg+YRy\/o=\",\"rid\":1},{\"uname\":\"hkIUZIikXVTC5aNaSva8IQ==\",\"passwd\":\"hkIUZIikXVTC5aNaSva8IQ==\",\"validupto\":\"\\\/Date(1545330600000)\\\/\",\"dept\":\"juubHSHgLr\/3JWnrZCh5LeeW5Q7lioWOZ1\/Tg+YRy\/o=\",\"rid\":2}]"}


I am trying to get the values in android application by using this Java code :

JSONObject uiobj = new JSONObject(mret);
JSONArray arrUserinfo = uiobj.getJSONArray("GetDataResult");


At the second line the code fails with this error :

.....  at GetDataResult of type java.lang.String cannot be converted to JSONArray


Please suggest how do I go about it ? Thanks in advance. Am i wrong from the c# side or java side.

What I have tried:

The similar code I have found everywhere and not able to get it to work. So I thought my JSON string requires a different method of approach and hence posted here. Im new to JSON, please bear ignorance.
Posted
Updated 21-Dec-17 23:35pm
v2
Comments
Richard MacCutchan 22-Dec-17 5:01am    
The data is being returned as two strings, separated by a colon. Those extra quote characters need to be removed first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900