简体   繁体   English

解码包含JSON字符串的JSON字符串

[英]Decode JSON string containing JSON string

I have to decode a JSON string containing another JSON string on it. 我必须解码包含另一个JSON字符串的JSON字符串。 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. 目前我正在尝试使用System.Web.Script.Serialization Serializator.Deserialize<Dictionary<string,string>>(value)将其解码为Dictionary<string,string> ,但是没有成功。

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... (System.ArgumentException HResult = 0x80070057 Message =传入的对象无效,':'或'}'预期。(34):{“label”:“Side”,“options”:“[{”key“:”left“ ,“value”:0},{“key”:“right”,“value”:1}]“} Source = System.Web.Extensions)这意味着他将”[{“作为字符串,因此无法转换当然...

Is there any way I can decode this specific JSON string and store it in an object? 有什么办法可以解码这个特定的JSON字符串并将其存储在一个对象中吗? Client is very specific about this JSON format... Thanks a lot 客户端对此JSON格式非常具体......非常感谢

Represent your json like that: 代表你的json

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

inside json with single quotes 在json里面用单引号

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: 所以你的json在关键的“选项”下有另一个json和string,你可以像这样提取它们:

 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);

PS here used Newtonsoft PS这里使用了牛顿软件

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 > . 并将所有JSON存储在Dictionary< string, object > I will then implement a method to decode the object inside the JSON . 然后,我将实现一个方法来解码JSON中的对象。

正如Matt在他的评论中已经提到的那样,你的JSON是无效的,而不是"[{"key"它应该是[{"key"而不是}]"}它应该是}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM