我有以下字符串
[{"id":"select","component":"select","editable":true,"index":2,
"label":"WHAT IS THIS QUESTION?","description":"some description",
"placeholder":"placeholder","options":["Aligator","Pikachi"],"required":false,
"validation":".*","$$hashKey":"object:38"}]
存储在数据库中。
我想检索validation
值。如何使用LINQ实现这一点?我正在使用EF Core 2,我无法在我的查询中取得进展。
var jsonData = from table in myRepository.Table
where table.JSONString.........
JSONString
是存储JSONString的列的名称。
我在SO和google上搜索帖子已经有一段时间了,但还没有找到答案。谢谢。
更新:
我为JSON创建了如下类
public class FormJson
{
[JsonProperty(PropertyName = "id")]
public string id { get; set; }
[JsonProperty(PropertyName = "component")]
public string component { get; set; }
...
}
但是我仍然不知道如何将这个特定的类映射到我的JSON对象并获得我需要的字段。
发布于 2018-07-28 08:00:17
下载NuGet-package Newtonsoft.Json
以包含using Newtonsoft.Json.Linq;
从数据库中获取JSON-string:
var jsonStr = (from t in myRepository.Table select t.JSONString).FirstOrDefault();
将JSON-string解析为JObject
JObject json = JObject.Parse(jsonStr);
读取要访问的特定令牌的值:
JToken validation = json.GetValue("validation");
对值做一些事情:
string value = validation.ToString();
JToken[] valueArray = validation.ToArray();
编辑:
由于在使用JSON (这是有效的)时JObject.Parse()
有问题,请尝试以下方法而不是使用JObject.Parse()
:
// Add references for TextReader and JsonReader
using Newtonsoft.Json;
using System.IO;
TextReader tReader = new StringReader(jsonStr);
JsonReader jReader = new JsonTextReader(tReader);
JObject json = JObject.Load(jReader);
JToken validation = json.GetValue("validation");
https://stackoverflow.com/questions/51566663
复制相似问题