任何人都可以提供一些建议,可能还有一个代码片段,使用C#和json.net来读取一些嵌套的json? 下面我的json使用JSONLint验证了良好的json。 我在解析json时遇到了障碍,我无法进入嵌套在根中的第3,第4,第6和第8个值。
[{
"interactionType": "MathEditorInteraction",
"interactionId": "08506178-22ba-4fa7-a490-c785716f10dc",
"value": "blah blah blah"
},
{
"interactionType": "MathEditorInteraction",
"interactionId": "1134871f-980e-4138-9598-0d4bf480aa97",
"value": "my first value"
},
{
"interactionType": "CanvasInteraction",
"interactionId": "89cd7bec-d0e8-4111-8442-f2ab95a1410b",
"value": "my second value"
},
{
"interactionType": "FillInBlankInteraction",
"interactionId": "7e9350b4-fb85-4f12-869e-227f99f77a73",
"value": "{\"results\":[{\"id\":\"1ac6770e-2093-4b7c-b595-789be8ee6efb\",\"value\":\"my third value\"}]}"
},
{
"interactionType": "FillInBlankInteraction",
"interactionId": "6f1ca6b7-3178-44a7-b8e9-e82d8c51d1fd",
"value": "{\"results\":[{\"id\":\"b7e92fd2-9c7a-4f71-88f9-e7d43e3179b7\",\"value\":\"my fourth value\"}]}"
},
{
"interactionType": "TextBoxInteraction",
"interactionId": "284c43f9-a268-4295-b96d-bc2f6dc30f0e",
"value": "my fifth value"
},
{
"interactionType": "FillInBlankInteraction",
"interactionId": "544b9907-139a-4c78-9671-502153be2697",
"value": "{\"results\":[{\"id\":\"f4e1ba6d-61dd-4eed-9c6f-dafc2701c161\",\"value\":\"my sixth value\"}]}"
},
{
"interactionType": "TextBoxInteraction",
"interactionId": "c0a5a1f0-2cae-42fd-8726-0ad36c11f413",
"value": "my seventh value"
},
{
"interactionType": "FillInBlankInteraction",
"interactionId": "ef6a7b62-8a7b-4b7f-b876-0d78ee6c4c87",
"value": "{\"results\":[{\"id\":\"af39469e-c041-4889-9e28-61a438cf56a3\",\"value\":\"my eight value\"}]}"
},
{
"interactionType": "TextBoxInteraction",
"interactionId": "f04de5b5-8a29-4200-a886-15f7dbd575b6",
"value": "my nineth value"
}]
然后我正在使用的一些c#:
JArray token = JArray.Parse(response); // response = json string above
for (int i = 0; i < token.Count; i++)
{
String value = token[i]["value"].ToString();
}
我可能会对我使用的JSON感到惊讶......它可能有n个计数的嵌套值...我的代码的目的是使用&#34;值&#34;来到最底层的孩子。串。 有没有办法查看令牌[i] [&#34;某些字符串&#34;]并查看它是否包含JArray或JObject以继续解析?
来自Timothy的建议编辑,我能够输出值。几乎就在那里。
static string json2 = @"[{""interactionType"": ""MathEditorInteraction"",""interactionId"": ""08506178-22ba-4fa7-a490-c785716f10dc"",""value"": ""blah blah blah""},{""interactionType"": ""MathEditorInteraction"",""interactionId"": ""1134871f-980e-4138-9598-0d4bf480aa97"",""value"": ""my first value""},{""interactionType"": ""CanvasInteraction"",""interactionId"": ""89cd7bec-d0e8-4111-8442-f2ab95a1410b"",""value"": ""my second value""},{""interactionType"": ""FillInBlankInteraction"",""interactionId"": ""7e9350b4-fb85-4f12-869e-227f99f77a73"",""value"": ""{\""results\"":[{\""id\"":\""1ac6770e-2093-4b7c-b595-789be8ee6efb\"",\""value\"":\""my third value\""}]}""},{""interactionType"": ""FillInBlankInteraction"",""interactionId"": ""6f1ca6b7-3178-44a7-b8e9-e82d8c51d1fd"",""value"": ""{\""results\"":[{\""id\"":\""b7e92fd2-9c7a-4f71-88f9-e7d43e3179b7\"",\""value\"":\""my fourth value\""}]}""},{""interactionType"": ""TextBoxInteraction"",""interactionId"": ""284c43f9-a268-4295-b96d-bc2f6dc30f0e"",""value"": ""my fifth value""},{""interactionType"": ""FillInBlankInteraction"",""interactionId"": ""544b9907-139a-4c78-9671-502153be2697"",""value"": ""{\""results\"":[{\""id\"":\""f4e1ba6d-61dd-4eed-9c6f-dafc2701c161\"",\""value\"":\""my sixth value\""}]}""},{""interactionType"": ""TextBoxInteraction"",""interactionId"": ""c0a5a1f0-2cae-42fd-8726-0ad36c11f413"",""value"": ""my seventh value""},{""interactionType"": ""FillInBlankInteraction"",""interactionId"": ""ef6a7b62-8a7b-4b7f-b876-0d78ee6c4c87"",""value"": ""{\""results\"":[{\""id\"":\""af39469e-c041-4889-9e28-61a438cf56a3\"",\""value\"":\""my eight value\""}]}""},{""interactionType"": ""TextBoxInteraction"",""interactionId"": ""f04de5b5-8a29-4200-a886-15f7dbd575b6"",""value"": ""my ninth value""}]";
var x = JsonConvert.DeserializeObject<Interaction[]>(json2);
for (int i = 0; i < x.Length; i++)
{
if (x[i].value.Contains("[{"))
{
var v = JsonConvert.DeserializeObject<Nested>(x[i].value);
Console.WriteLine(v.results[0].value);
}
else
{
Console.WriteLine(x[i].value);
}
}
控制台输出:
blah blah blah
my first value
my second value
my third value
my fourth value
my fifth value
my sixth value
my seventh value
my eight value
my ninth value
仍然坚持实际检测到比第一级更深的阵列。 你可以看到我的黑客寻找&#34; [{&#34;这不是首选。
编辑:KUDOS向Jens帮助我实现了我今天所需的解决方案。有关完整说明,请参阅下面的评论。答案 0 :(得分:2)
如果您不希望进行反序列化对象,则可以对JToken.Type作出反应。 我实际上大部分都是在C#我自己的动态时代使用JSON,从不将它反序列化为强类型,这主要是因为我们使用后端,其中数据结构由客户端定义,我们需要能够处理更多动态数据。
https://dotnetfiddle.net/awUSGT
dynamic arr = JArray.Parse(JSON); // response = json string above
foreach (dynamic token in arr)
{
JTokenType type = ((JToken)token.value).Type;
switch (type)
{
case JTokenType.String:
Console.WriteLine(token.value);
break;
case JTokenType.Object:
Console.WriteLine(token.value.results.Last.value);
break;
}
}
请注意,对于8.x或9.x之前(当我发布该修复时无法记住),上面的内容会在转换为JToken时抛出异常。
所以可以这样做:
dynamic arr = JArray.Parse(JSON); // response = json string above
foreach (JObject token in arr)
{
dynamic value = token["value"];
switch (token["value"].Type)
{
case JTokenType.String:
Console.WriteLine(value);
break;
case JTokenType.Object:
Console.WriteLine(value.results.Last.value);
break;
}
}
至于最后一点,我用来美化你的JSON的工具似乎剥夺了一些逃避你的价值观。
我不知道您是否有意将嵌入的JSON作为字符串(序列化JSON),如果是这样,您需要找到一种方法来发现它,然后解析它。
松散/放松的方法可能是:
dynamic arr = JArray.Parse(JSON); // response = json string above
foreach (dynamic token in arr)
{
string tokenvalue = (string) token.value;
JToken value = Regex.IsMatch(tokenvalue, "^\\{.*\\}$")
? JToken.Parse(tokenvalue)
: token.value;
switch (value.Type)
{
case JTokenType.String:
Console.WriteLine(value);
break;
case JTokenType.Object:
Console.WriteLine(((dynamic)value).results.Last.value);
break;
}
}
因为我不确切地知道你的&#34;递归嵌套&#34;看起来,很难猜测,但有些东西:
public static void Main()
{
dynamic arr = JArray.Parse(JSON); // response = json string above
foreach (dynamic token in arr)
{
JToken value = ExtractValue(token);
Console.WriteLine(value);
}
}
private static JToken ExtractValue(dynamic token)
{
string tokenvalue = (string) token.value;
JToken value = Regex.IsMatch(tokenvalue, "^\\{.*\\}$")
? JToken.Parse(tokenvalue)
: token.value;
switch (value.Type)
{
case JTokenType.String:
return value;
case JTokenType.Object:
return ExtractValue(((dynamic) value).results.Last);
default:
throw new InvalidOperationException("Could not extract data, unknown json construct.");
}
}
可能。
答案 1 :(得分:0)
尝试使用此:JsonConvert.DeserializeObject<Interaction[]>(yourJson);
其中Interaction
是:
public class Interaction
{
public string interactionType {get;set;}
public string interactionId {get;set;}
public string value {get;set;}
}
然后,对于具有嵌套JSON的那些,您可以简单地解析它们的value
属性,如下所示:
public class Nested
{
public Result[] results {get;set;}
}
public class Result
{
public string id {get;set;}
public string value {get;set;}
}
然后解析它:
var v = JsonConvert.DeserializeObject<Nested>(x[8].value);
答案 2 :(得分:0)
您可以使用this service
轻松生成模型类使用此模型,您可以使用Newtonsoft Json轻松反序列化