2

This is my C# webservice which generates a JSON String. The below code block is what im using for that.

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 is called in a java code from android studio and it returns the below String.

{"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");
arrUserinfo.getJSONObject(0).getString("uname"))

The code fails at the second line. I'm new to JSON. Not sure if the JSON generated from c# code is not right or java code for parsing is not right. Please advise further. Thank you in advance.

Priya
  • 77
  • 1
  • 12
  • The code fails at the second line? Post stacktrace? – Aswin P Ashok Dec 23 '17 at 05:36
  • Your json data is not well formatted. Thats why it fails. I think the escape characters are doing this. Go to https://jsonformatter.org/ and paste your json data there to see weather it is formatted right or not – Aswin P Ashok Dec 23 '17 at 05:39
  • It seems like portions of your JSON have been double-serialized, along the lines of [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179) -- even though you're using `JavaScriptSerializer` not `Json.NET`. Can you show how you return your JSON from your webservice? – dbc Dec 23 '17 at 05:41
  • @AswinPAshok Thank you for the response. I referred jsonformatter.org and found that JSON is not in valid format. Thank you. – Priya Dec 23 '17 at 06:01
  • @dbc Thank you for your response. This is how Iam returning from WebService. – Priya Dec 23 '17 at 06:02
  • @dbc Thank you for your response. I have shown the code which I use in the webservice. It is a List> type object im using and filling up the list with entries in the table, finally using JavaScriptSerializer jss = new JavaScriptSerializer(); String mret = jss.Serialize(lstdict); and returning this string back to the caller. – Priya Dec 23 '17 at 06:09
  • Can anyone help me to correct the JSON output from C# – Priya Dec 23 '17 at 06:17
  • Take a look at this [question](https://stackoverflow.com/questions/27897894/serialise-listdictionarystring-object) – Aswin P Ashok Dec 23 '17 at 07:31
  • @AshwinPAshok lstdict is a collection so i cannot return that.. Thanks. – Priya Dec 23 '17 at 07:38
  • did you check this question? https://stackoverflow.com/questions/27897894/serialise-listdictionarystring-object – Aswin P Ashok Dec 23 '17 at 07:50

1 Answers1

1

The above json is serialized you need to parse json and then extract the object from it.

See this json valid..

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

To parse json in java see below post..

https://www.geeksforgeeks.org/parse-json-java/

Dhaval Asodariya
  • 558
  • 5
  • 19
  • Thank you so much for help. I got the mistake in my JSON String. But it is automatically generated from c# class. Yet JSONParser returns an unknown symbol. – Priya Dec 23 '17 at 06:15
  • Ok. I got the JSONParser working. I think I need to correct the JSON Output from C# first to the one you have shown here. I will try to do that. Thank you. – Priya Dec 23 '17 at 06:31
  • Yes json output is serialized. – Dhaval Asodariya Dec 23 '17 at 07:35