1

I am trying to deserialize a JSON string but I get an error:

var response = jss.Deserialize<Dictionary<string,string>>(responseValue); 

I got an error that :

Type 'System.String' is not supported for deserialization of an array.

I think that the error will be fixed if I change \" with '

this is the string

"{\"data\":[],\"error\":1,\"error_msg\":\"could not find associated database\",\"message\":\"Please check sr_no that you have sent\"}"

I want it like this

"{'data':[],'error':1,'error_msg':'could not find associated database','message':'Please check sr_no that you have sent'}"

I have tried this using function as following but didn't work for me

responseValue.Replace("\"","'");
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
SaMeEr
  • 361
  • 2
  • 7
  • 22
  • 2
    Are you _sure_ that's what the string is? It looks like it's just a debugger representation of it. Also, that looks like it's JSON, why go through strange string manipulations of it (especially ones that make it _invalid_ JSON), and not just treat it as JSON properly? – James Thorpe May 04 '17 at 07:42
  • 1
    Your code works https://dotnetfiddle.net/nwqUqM – fubo May 04 '17 at 07:42
  • Yes it is a debugger representation @JamesThorpe. what actually happening is I am deserializing josn using var response = jss.Deserialize>(responseValue); but I got an error that "Type 'System.String' is not supported for deserialization of an array.". – SaMeEr May 04 '17 at 07:49
  • In that case, this very much looks like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - why not ask about your actual problem instead? – James Thorpe May 04 '17 at 07:51
  • I was considering that problem is in string because when I am passing the json like this: var response = jss.Deserialize>("{'name':'John','age':'23'}"); then this is working fine. so I thought it is a problem with string manipulation. – SaMeEr May 04 '17 at 07:56
  • What can be the issue @JamesThorpe – SaMeEr May 04 '17 at 07:57
  • @SaMeEr there's nothing wrong with the string. That's just a normal JSON string. Json has no preference for `'` over `"`. Post your code. – Panagiotis Kanavos May 04 '17 at 08:37
  • @SaMeEr replacing the quote character can only *cause* errors if some string value already contains embedded single quotes. It won't fix anything – Panagiotis Kanavos May 04 '17 at 08:40
  • @SaMeEr the error explains what's wrong. You are trying to deserialize an array (`data`) into a string. – Panagiotis Kanavos May 04 '17 at 09:22
  • I got it @PanagiotisKanavos – SaMeEr May 04 '17 at 09:35

5 Answers5

4

If you are expecting changes in same variable then you need to set it again with returned results.

responseValue = responseValue.Replace(@"\"","'");
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • 2
    This should either be `.Replace(@"\""","'")`, `.Replace(@"""","'")` or `.Replace("\"","'")`. The current code does not compile. – Kobi May 04 '17 at 08:04
  • Replacing the quote character isn't going to fix any Json deserialization errors. It can *cause* them though, if the string contained embedded single quotes – Panagiotis Kanavos May 04 '17 at 08:36
  • Agreed, but the question will be taken on face value. OP will get what he needs from this answer. How this fits in the bigger picture needs to be handled by him. – Nikhil Agrawal May 04 '17 at 08:46
  • On face value, this still doesn't help in any way *because the OP is already replacing the double quote correctly * and still has a problem `I have tried this using function as following but didn't work for me: responseValue.Replace("\"","'");`. In fact, this answer uses the wrong escaping and won't compile – Panagiotis Kanavos May 04 '17 at 09:10
  • That was before question was closed. If its a new query, a new question needs to be raised. – Nikhil Agrawal May 04 '17 at 10:07
0

try this :

String s = "{\"data\":[],\"error\":1,\"error_msg\":\"could not find associated database\",\"message\":\"Please check sr_no that you have sent\"}";

s= s.Replace("\"", "'");
Y.Alyaprak
  • 26
  • 3
0
string responseValue = "{\"data\":[],\"error\":1,\"error_msg\":\"could not find associated database\",\"message\":\"Please check sr_no that you have sent\"}";
Console.WriteLine(responseValue.Replace("\"", "'"));

Check the output

If you want to return this value, Then save it in a variable and return that variable. Hope my answer helped you. If anything comment below.

Dilip Kumar
  • 15
  • 1
  • 7
0

The error message explains the problem: You are trying to deserialize a string that contains array properties into a dictionary of strings. You can't put an array into a string, so Type 'System.String' is not supported for deserialization of an array..

Specifically, the data property is an empty array:

'data':[]

This has nothing to do with the quote character. JSON works fine with either single or double characters.

You need to provide an appropriate type for deserialization. You can deserialize the properties to object, dynamic or create a class that matches the JSON text, eg:

var response = jss.Deserialize<Dictionary<string,object>>(responseValue); 

Or :

class MyError
{
    public string[] data{get;set;}
    public string error_msg {get;set;}
    public string message {get;set;}
}

var response = jss.Deserialize<MyError>(responseValue); 
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

You can improving this.

    static private T CleanJson<T>(string jsonData)
    {
        var json = jsonData.Replace("\t", "").Replace("\r\n", "");
        var loop = true;
        do
        {
            try
            {
                var m = JsonConvert.DeserializeObject<T>(json);
                loop = false;
            }
            catch (JsonReaderException ex)
            {
                var position = ex.LinePosition;
                var invalidChar = json.Substring(position - 2, 2);
                invalidChar = invalidChar.Replace("\"", "'");
                json = $"{json.Substring(0, position -1)}{invalidChar}{json.Substring(position)}";
            }
        } while (loop);
        return JsonConvert.DeserializeObject<T>(json);
    }

Example;

var item = CleanJson<ModelItem>(jsonString);
dcansyn
  • 78
  • 6