如何使用JavaScriptSerializer解析多层嵌套的JSON数组
Got it, let's break this down step by step—first fixing your JSON syntax, then showing you how to properly parse that nested array with JavaScriptSerializer.
Fixing Your JSON Format
The main issue with your JSON is a missing colon : between the "data" key and its array value. Here's the corrected, fully valid version:
var jsonText = @"{ ""data"": [ { ""some_number"": 108.541, ""date_time"": ""2011-04-13T15:34:09Z"", ""serial_number"": ""SN1234"", ""more_data"": { ""field1"": 1.0, ""field2"": ""hello"" }, ""Hobbies"" : [""game1"",""game2"",""game3""] }, { ""some_number"": 200.541, ""date_time"": ""2012-04-11T15:34:09Z"", ""serial_number"": ""SN3333"", ""more_data"": { ""field1"": 2.0, ""field2"": ""hello1"" }, ""Hobbies"" : [""game4"",""game5"",""game6""] } ] }";
All brackets, colons, and quotes are properly paired now, so this will parse without syntax errors.
Parsing the Nested JSON Array
You have two reliable options here—using dynamic (similar to your original approach) or strongly-typed classes (more maintainable and type-safe). Let's cover both:
Option 1: Using dynamic (Quick & Flexible)
Since "data" is an array, when you deserialize to dynamic, dict["data"] will be a List<dynamic> that you can iterate over or access by index. Here's how to use it:
var jss = new JavaScriptSerializer(); var dict = jss.Deserialize<dynamic>(jsonText); // Access the first item in the data array var firstItem = dict["data"][0]; Console.WriteLine(firstItem["some_number"]); // Output: 108.541 Console.WriteLine(firstItem["more_data"]["field2"]); // Output: hello Console.WriteLine(string.Join(", ", firstItem["Hobbies"])); // Output: game1, game2, game3 // Loop through all items in the array foreach (var item in dict["data"]) { Console.WriteLine($"Serial Number: {item["serial_number"]}"); Console.WriteLine($"Hobbies: {string.Join(", ", item["Hobbies"])}"); }
This works great for small scripts, but note there's no compile-time type checking—typos in property names will cause runtime errors.
Option 2: Using Strongly-Typed Classes (Type-Safe & Recommended for Production)
Define classes that match your JSON structure. This gives you IntelliSense, compile-time validation, and avoids accidental mistakes. Here's how:
First, create the matching classes:
public class RootObject { public List<DataItem> data { get; set; } } public class DataItem { public double some_number { get; set; } public DateTime date_time { get; set; } public string serial_number { get; set; } public MoreData more_data { get; set; } public List<string> Hobbies { get; set; } } public class MoreData { public double field1 { get; set; } public string field2 { get; set; } }
Then deserialize and work with the strongly-typed objects:
var jss = new JavaScriptSerializer(); var root = jss.Deserialize<RootObject>(jsonText); // Access the first item var firstItem = root.data[0]; Console.WriteLine(firstItem.some_number); // 108.541 Console.WriteLine(firstItem.more_data.field2); // hello Console.WriteLine(string.Join(", ", firstItem.Hobbies)); // game1, game2, game3 // Iterate all items foreach (var item in root.data) { Console.WriteLine($"Date: {item.date_time:yyyy-MM-dd}"); Console.WriteLine($"Field1 Value: {item.more_data.field1}"); }
This is the better choice for larger projects where data consistency and maintainability matter.
内容的提问来源于stack exchange,提问作者Mist

