Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to deselarize a json string in c#.
i am taking the values for the respective json elements and getting exception below:

Invalid object passed in, ':' or '}' expected json serialize c#

What I have tried:

Building Json Code as below:
 string json = @"  
   [
    {
       
        {                                         
        ""bankId"": """ + hsk.bankid + @""",
        ""password"": """ + hsk.password + @"""                                         
        } 

     
 }
]
";

Output

   [
    {
       
        {                                         
        "bankId": "011",
        "password": "test123"                                         
        } 

     
 }
]

Deserializing as below:
<pre>   HandShakeRequest resultT = new JavaScriptSerializer().Deserialize<HandShakeRequest>(json);
Posted
Updated 18-Jun-18 15:11pm
v3

Try removing one set of curly brackets:
[{"bankId":"011","password":"test123"}]
But ... plain text passwords? Not a good idea ... Code Crime 1[^]
Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
 
Share this answer
 
Quote:
[
{

{
"bankId": "011",
"password": "test123"
}


}
]


That is an invalid JSON string. You may check it using, for instance JSONLint[^]. See also JSON Objects at w3schools[^].
 
Share this answer
 
As they already metioned, your JSON string is invalid that's why you are getting an error. To prevent invalid JSON string format, you could write an extension method or a static method to check of validity. For example:

C#
public static bool IsValidJson(string strInput)
        {
            strInput = strInput.Trim();
            if ((strInput.StartsWith("{") && strInput.EndsWith("}")) || //For object
                (strInput.StartsWith("[") && strInput.EndsWith("]"))) //For array
            {
                try
                {
                    var obj = JToken.Parse(strInput);
                    return true;
                }
                catch (JsonReaderException jex)
                {
                    //Exception in parsing json
                    //jex.Message;
                    return false;
                }
                catch (Exception ex) //some other exception
                {
                    //ex.ToString()
                    return false;
                }
            }
            else
            {
                return false;
            }
        }


You can then do:

C#
var isValid = IsValidJson(jsonString);

if(isValid){
   //deserialize here
}


Also keep note of the security risk as stated by OriginalGriff.
 
Share this answer
 

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