2017-04-18 118 views
1

在javascript这question JSON.parse(json)将验证失败 在c#中没有任何替代方案时,没有为json对象中的名称提供引号。 (地名指示键值对的键)无效的JSON对象的键不带引号在C#

{ 
    name: "s" 
} 

应验证失败,其中作为

{ 
    "name": "s" 
} 

应该通过验证

与Newtonsoft.Json的JObject.Parse(身体)审判;但它会自动添加引号并通过验证。 我想在c#中根据JSON标准RFC 4627进行验证。想知道是否有设施未通过验证的情况下不提供c#中的密钥引号

回答

1

终于找到了我自己的解决方案。

public bool ValidateMissingDoubleQuotes(string json) 
    { 
     using (var reader = new JsonTextReader(new StringReader(json))) 
     { 
      while (reader.Read()) 
      { 
       return !(reader.TokenType == JsonToken.PropertyName && reader.QuoteChar != '\"'); 
      } 
     } 
     return true; 
    }