c# - 使用 json.NET 和 C# 解析 JSON 数组

标签 c# arrays json parsing json.net

我需要解析以下 JSON 格式的一些数据:

{
"status":0,
"timestamp":"8:20pm",
"routes":[
    {
        "directions":[
            "E Towne",
            "ETP"
        ],
        "routeID":"30"
    },
    {
        "directions":[
            "Johnson",
            "Observatory"
        ],
        "routeID":"81"
    }
]
}

使用 json.net,我想获得以下输出:

30 E Towne – ETP

81 Johnson – Observatory

使用下面的代码,我得到以下不正确的输出:

30 E Towne – ETP

81 E Towne – ETP

如何将方向数组项写出到相应的 routeID 项?我的代码:

public class Route
        {
            public string routeID { get; set; }
            public string directions { get; set; }  
        }

private void routeClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
        {
            string jsonResults_routes = e.Result;
            JObject routeData = JObject.Parse(jsonResults_routes);

            JArray routeIdArray = (JArray)routeData["routes"];
            JArray routeDirections = (JArray)routeData["routes"][0]["directions"];

            List<Route> l = new List<Route>();

            for (int i = 0; i < routeIdArray.Count; i++)
            {
                Route BusRoutes = new Route();

                JObject routeIDarrayObject = (JObject)routeIdArray[i];
                BusRoutes.routeID = (string)routeIDarrayObject["routeID"];

                string sep = " - ";
                string bothDirections = String.Join(sep, routeDirections);

                List<string> sc = new List<string>();
                string[] direction = new string[]{bothDirections};
                sc.AddRange(direction);

                foreach (string direx in sc)
                {
                    BusRoutes.directions = direx; 
                }

                l.Add(BusRoutes);

            }
            var newList = l.OrderBy(x => x.routeID).ToList();
            lbRoutesData.ItemsSource = newList;
        }

最佳答案

@competent_tech 的分析是正确的。如果我可以提议,我认为如果您使用实际对象会感觉更自然。例如:

public class RouteInfo
{
    public List<string> Directions { get; set; }
    public string RouteID { get; set; }
}

public class RouteData
{
    public int Status { get; set; }
    public string Timestamp { get; set; }
    public List<RouteInfo> Routes { get; set; }
}

在你的方法中:

var routeData = JsonConvert.DeserializeObject<RouteData>(e.Result);
return routeData.Routes
                .Select(r => new Route
                    {
                        routeId = r.RouteID,
                        directions = String.Join(" - ", r.Directions)
                    })
                .OrderBy(r =­> r.routeId)
                .ToList();

或者以其他方式更自然地操作您的类型对象。

编辑:有关基于 JSON 字符串生成类的简单方法,请转至 json2csharp .

关于c# - 使用 json.NET 和 C# 解析 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17493906/

相关文章:

ios - 如何通过单击 UITableViewCell 按钮将数据发送到另一个 ViewController

c# - 如何在列表中使用 "Contain"?

c# - 使用 LINQ 和 C# 比较集合并计算重复项

c# - .NET C# ComboBox 默认文本

c# - System.NullReferenceException 未将对象引用设置为对象的实例

java - 如何遍历字节数组列表...?

c - 尝试将 3D 空间中的点 append 到数组末尾时出现段错误

c# - 如何从 C# 中的数组中删除范围

c# - 告诉 Json.Net 在序列化对象时编写单引号而不是双引号

c# 无法反序列化当前 JSON 对象