4

I have to decode a JSON string containing another JSON string on it. Currently I'm trying to decode it into a Dictionary<string,string> using Serializator.Deserialize<Dictionary<string,string>>(value) from System.Web.Script.Serialization, but haven't succeed.

This is the string:

{
      "label": "Side",
      "options": [
        {
          "key": "left",
          "value": 0
        },
        {
          "key": "right",
          "value": 1
        }
      ]
}

And this is the format error I get from the decoder:

(System.ArgumentException HResult=0x80070057 Message=Invalid object passed in, ':' or '}' expected. (34): {"label": "Side", "options": "[{"key": "left", "value": 0},{"key":"right", "value":1}]"} Source=System.Web.Extensions) Which means he gets "[{" as a string and thus fails to convert of course...

Is there any way I can decode this specific JSON string and store it in an object? Client is very specific about this JSON format... Thanks a lot

4
  • 3
    Seems like a pain that you are receiving invalid json...... Commented Oct 30, 2018 at 10:19
  • You may have to pre-parse the json into something that can be passed to a deserialiser. Commented Oct 30, 2018 at 10:35
  • There is a link to jsonlint In the tag info. It is a good tool for validation and formatting. Commented Oct 30, 2018 at 10:58
  • JSON string is not valid. Please get it corrected using jsonlint. Commented Oct 30, 2018 at 12:49

3 Answers 3

2

Represent your json like that:

{
  "label": "Side",
  "options": "[{ 'key': 'left', 'value': '0'},{ 'key':'right', 'value':1}]"
}

inside json with single quotes

let's assume you have this two classes :

public class YourObject
    {
        public string label { get; set; }
        public string options { get; set; }
    }
    public class InsideObject
    {
        public string key { get; set; }
        public int value { get; set; }
    }

so your json has another json as as string under the key "options" and you can extract both of them like that:

 string json = "{\"label\": \"Side\", \"options\": \"[{ 'key': 'left', 'value': '0'},{ 'key':'right', 'value':1}]\"}";
 var jsonObj = JsonConvert.DeserializeObject<YourObject>(json);
 var insideObj = JsonConvert.DeserializeObject<InsideObject>(jsonObj.options);

P.S here used Newtonsoft

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

3 Comments

Doesn't work... as soon as it gets to the first single cuote, the deserializer thinks that's the end of the string and throws an error because it is an invalid way of ending a JSON string
Proposing to Represent your json like that (and storing an object in form of a string) is in my opinion on the same level as using Object everywhere :D
I agree with you. I just answered his his question: "...string containing another JSON string on it".
0

Finally I used string format as follows:

{
  "label": "Side",
  "options": [
    {
      "key": "left",
      "value": 0
    },
    {
      "key": "right",
      "value": 1
    }
  ]
}

and store all the JSON in a Dictionary< string, object >. I will then implement a method to decode the object inside the JSON.

Comments

-1

As Matt already mentioned in his comment your JSON is invalid, instead of "[{"key" it should be [{"key" and instead of }]"} it should be }]}.

1 Comment

I have no problems with down votes at all as long as I can learn something from them (which is not possible without any comment).

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.