添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爱运动的毛豆  ·  spring-boot-starter-va ...·  18 小时前    · 
没有腹肌的黑框眼镜  ·  spring mvc +@Valid ...·  18 小时前    · 
爱听歌的风衣  ·  C#: Validate a ...·  16 小时前    · 
礼貌的萝卜  ·  error C2039: ...·  13 小时前    · 
重情义的蟠桃  ·  java ...·  8 小时前    · 
爱听歌的水桶  ·  WPF ...·  1 月前    · 
奋斗的奔马  ·  PHP cURL: ...·  6 月前    · 
" Name" : " source" , " Value" : " 0" , " VariableType" : " ServiceData.Contracts.Validation.Source" " Registers" : [ " Label" : " DKE" , " ReadingTime" : " /Date(9/21/20)/" , " Source" : " Local" , " StatusFlags" : null , " Value" : 1 I'm fairly new to C# and JSON, but I've tried using
JToken token = JToken.Parse(table); JArray entries = (JArray)token.SelectToken( " Parameters[0].Name" );
However that doesn't seem to work since it gives me back a compilation error that reads,
Unable to cast object of type ' Newtonsoft.Json.Linq.JValue' to type ' Newtonsoft.Json.Linq.JArray' . '
Despite the fact that I cast (JArray) to token
What I have tried:
public static List<string> CleanOperationsTable( string liteConString) List<string> cleanedList = new List<string>(); SQLiteConnection liteCon = new SQLiteConnection(liteConString); liteCon.Open(); DataTable dtbl = new DataTable(); string select = " SELECT ResponseBody FROM Operations LIMIT 1" ; SQLiteCommand cmd = new SQLiteCommand( select , liteCon); SQLiteDataAdapter sqliteDAP = new SQLiteDataAdapter(cmd); sqliteDAP.Fill(dtbl); /// Converts the 1 column DataTable into a single string to /// it's easier to parse with JSON string table = string .Join(Environment.NewLine, dtbl.Rows.OfType<DataRow>().Select(x => string .Join( " ; " , x.ItemArray))); JToken token = JToken.Parse(table); JArray entries = (JArray)token.SelectToken( " Parameters[0].Name" ); foreach (JToken t in entries) Console.WriteLine(t); return cleanedList; I tried following these answers: c# - How to parse nested JSON data structure - Stack Overflow [ ^ ]
However that doesn't seem to work for me since their JSON statements has 1 more layer of wrapping than mine. Is there a library that allows me to parse through both nested and un-nested JSON strings? I'm currently using Newtonsoft.JSON package. Parameters[0].Name is a JValue . Specifically it's the string "ESPN" given the example JSON. JValue has implementations for converting common types like strings so you can do (string)jValueThatIsAString . But the value in your example will never be a JArray which is why you get the error.
I'm not really sure what you mean by "nested and un-nested" since there are no nesting differences in your sample JSON. I'm guessing you want to be able to aggregate all Name properties of objects in an array. You can do it like this:
JToken token = JToken.Parse( @" ""OperationID"": 7, ""Outcome"": 0, ""Parameters"": [ ""Name"": ""ESPN"", ""Value"": ""342.342.444"", ""VariableType"": ""System.String"" ""Name"": ""source"", ""Value"": ""0"", ""VariableType"": ""ServiceData.Contracts.Validation.Source"" }" ); // with LINQ IEnumerable<string> entries = token .SelectToken( " Parameters" ) .Select(p => ( string )p.SelectToken( " Name" )); // or IEnumerable<JToken> entries = token. .SelectToken( " Parameters" ) .Select(p => p.SelectToken( " Name" )); // or with JSONPath IEnumerable<JToken> entries = token.SelectTokens( " $.Parameters[*].Name" ); // If you were looking for getting the objects out (not just the name): IEnumerable<JToken> entries = token.SelectTokens( " $.Parameters[*]" );
References:
Newtonsoft.Json.Linq Namespace [ ^ ]
LINQ to JSON [ ^ ]
JSONPath - XPath for JSON [ ^ ]
EDIT: For fun, if you do have objects with a Name property arbitrarily nested within an array called Parameters that can contain subarrays, you could retrieve all Name properties using this:
IEnumerable<JToken> entries = token.SelectTokens( " $.Parameters..Name" ); // or if you want to be explicit that Parameters is an array IEnumerable<JToken> entries = token.SelectTokens( " $.Parameters[*]..Name" ); // the recursive descent operator ".." doesn't care unlike the child operator "."
This works regardless of the nesting depth of the subarrays or objects.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •