2009-09-14 56 views
5

我看着这个Parsing JSON using Json.net问题和答案,它是靠近我需要什么。关键的区别是,我需要解析形成每记录一个或多个行的x,y对的阵列。下面是我输入的例子JSON.NET和数组使用LINQ

{ 
"displayFieldName" : "FACILITYID", 
"fieldAliases" : { 
"FACILITYID" : "Facility Identifier", 
}, 
"geometryType" : "esriGeometryPolyline", 
"spatialReference" : { 
    "wkid" : 4326 
}, 
"features" : [ 
{ 
    "attributes" : { 
    "FACILITYID" : "", 
    "OBJECTID" : 1, 
    }, 
    "geometry" : 
    { 
    "paths" : 
    [ 
     [ 
     [-80.3538239379999, 27.386884271], 
     [-80.3538100319999, 27.3868901900001], 
     [-80.3538157239999, 27.3869008510001] 
     ] 
    ] 
    } 
}, 
{ 
    "attributes" : { 
    "FACILITYID" : "", 
    "OBJECTID" : 2, 
    }, 
    "geometry" : 
    { 
    "paths" : 
    [ 
     [ 
     [-80.3538239379999, 27.386884271], 
     [-80.3538295849999, 27.3868948420001] 
     ] 
    ] 
    } 
} 
] 
} 

(查看http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/WaterTemplate/WaterDistributionNetwork/MapServer/9/query?outFields= * &其中= OBJECTID%3C20 & F = pjson的完整清单)

我需要做的是解析[ “功能”] [“几何”] [“路径”]阵列中为x的组成的线,y对。这是我应得的所有路径(每一个“记录”的功能阵列中):

var allPaths = from p in jsonObject["features"].Children()["geometry"] 
       select p["paths"]; 

这给了我我的路径,从中我可以再处理每个点阵列依次为:

foreach (var eachPolylineInPath in allPaths) 
{ 
    IEnumerable<Point> linePoints = from line in eachPolylineInPath.Children() 
            select new Point(
                (double) line[0], 
                (double) line[1], 
                double.NaN); 
} 

这就是我会被卡住。我试图从JArray和LINQ-Y陈述各种铸件,但不断收到空结果或例外JProperty子值的调整不能被访问。

希望有人已经处理了转换使用LINQ在JSON.NET数组的数组,可以解释愚蠢的错误我必须做,或者明显的答案,我没有看到。

回答

9

貌似路径是点的数组的数组,所以假设你要一个IEnumerable每个路径,需要:

var allPaths = from p in jsonObject["features"].Children()["geometry"] 
       select p["paths"].Children(); 
+0

你钉它 - 我失踪儿童()在p [”路径“],所以我4小时搜寻真相已经完成。非常感谢。 – Dylan 2009-09-14 23:05:45