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.

10
  • The code fails at the second line? Post stacktrace? Commented Dec 23, 2017 at 5:36
  • Your json data is not well formatted. Thats why it fails. I think the escape characters are doing this. Go to jsonformatter.org and paste your json data there to see weather it is formatted right or not Commented Dec 23, 2017 at 5: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 -- even though you're using JavaScriptSerializer not Json.NET. Can you show how you return your JSON from your webservice? Commented Dec 23, 2017 at 5:41
  • @AswinPAshok Thank you for the response. I referred jsonformatter.org and found that JSON is not in valid format. Thank you. Commented Dec 23, 2017 at 6:01
  • @dbc Thank you for your response. This is how Iam returning from WebService. Commented Dec 23, 2017 at 6:02

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
Yes json output is serialized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.