C# 如何使用C将JSON转换为对象#
我在访问json文件中的属性时遇到问题。我还需要用它创建一个c#对象。它不能正常工作。这需要深入研究几个类,在这些类中我找不到任何其他文档,因为大多数都使用非常简单的json文件C# 如何使用C将JSON转换为对象#,c#,C#,我在访问json文件中的属性时遇到问题。我还需要用它创建一个c#对象。它不能正常工作。这需要深入研究几个类,在这些类中我找不到任何其他文档,因为大多数都使用非常简单的json文件 { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "TYPE":
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties":
{
"TYPE": "COASTAL",
"R_STATEFP": "28",
"L_STATEFP": ""
},
"geometry":
{
"type": "LineString",
"coordinates": [
[ -88.453654, 30.196584 ],
[ -88.428301, 30.198511 ],
[ -88.404581, 30.206162 ],
[ -88.401466, 30.210172 ],
[ -88.430332, 30.208548 ],
[ -88.442654, 30.202314 ],
[ -88.453444, 30.201236 ],
[ -88.465713, 30.202540 ],
[ -88.500011, 30.214044 ],
[ -88.506999, 30.214348 ],
[ -88.502752, 30.210506 ],
[ -88.493523, 30.205945 ],
[ -88.453654, 30.196584 ]
]
}
},
//repeated 100+ times
]
}
这是我的类文件:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MyApp
{
public class FeatureCollection
{
public string type{ get; set; }
public List<Feature> features{ get; set; }
[JsonConstructor]
public FeatureCollection(JObject i)
{
var typeProp = i.GetType().GetProperty("type");
this.type = typeProp.GetValue(i) as string;
JArray features = (JArray)i.GetValue("features");
this.features = new List<Feature>();
foreach (JObject f in features)
{
this.features.Add(new Feature(f));
Console.Write(features);
}
}
}
public class Feature
{
public string type;
public Property properties;
public Geometry geometry;
[JsonConstructor]
public Feature(JObject i)
{
var typeProp = i.GetType().GetProperty("type");
this.type = typeProp.GetValue(i) as string;
var prop = i.GetValue("properties") as JObject;
this.properties = new Property(prop);
var geo = i.GetValue("geometry") as JObject;
this.geometry = new Geometry(geo);
}
}
public class Property
{
public string TYPE;
public string R_STATEFP;
public string L_STATEFP;
[JsonConstructor]
public Property(JObject i)
{
var typeProp = i.GetType().GetProperty("TYPE");
this.TYPE = typeProp.GetValue(i) as string;
var typeR = i.GetType().GetProperty("type");
this.R_STATEFP = typeR.GetValue(i) as string;
var typeL = i.GetType().GetProperty("type");
this.L_STATEFP = typeL.GetValue(i) as string;
}
}
public class Geometry
{
public string type;
public List<Coord> coordinates;
[JsonConstructor]
public Geometry(JObject i)
{
var typeProp = i.GetType().GetProperty("type");
this.type = typeProp.GetValue(i) as string;
JArray coordinates = (Newtonsoft.Json.Linq.JArray)i.GetValue("coordinates");
this.coordinates = new List<Coord>();
foreach (JArray c in coordinates)
{
this.coordinates.Add(new Coord(c));
}
}
}
public class Coord
{
public double longitude;
public double latitude;
[JsonConstructor]
public Coord(JArray a){
this.longitude = (double)a[0];
this.latitude = (double)a[1];
}
}
}
使用系统;
使用System.Collections.Generic;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
名称空间MyApp
{
公共类功能集合
{
公共字符串类型{get;set;}
公共列表功能{get;set;}
[JsonConstructor]
公共特征收藏(JObject i)
{
var typeProp=i.GetType().GetProperty(“类型”);
this.type=typeProp.GetValue(i)作为字符串;
JArray特性=(JArray)i.GetValue(“特性”);
this.features=新列表();
foreach(功能中的作业对象f)
{
此.features.Add(新特性(f));
控制台。编写(功能);
}
}
}
公共类功能
{
公共字符串类型;
公共财产;
公共几何;
[JsonConstructor]
公共专题(JObject i)
{
var typeProp=i.GetType().GetProperty(“类型”);
this.type=typeProp.GetValue(i)作为字符串;
var prop=i.GetValue(“属性”)作为作业对象;
this.properties=新属性(prop);
var geo=i.GetValue(“几何图形”)作为作业对象;
this.geometry=新几何体(geo);
}
}
公共类财产
{
公共字符串类型;
公共字符串R_STATEFP;
公共字符串L_STATEFP;
[JsonConstructor]
公共财产(项目一)
{
var typeProp=i.GetType().GetProperty(“类型”);
this.TYPE=typeProp.GetValue(i)作为字符串;
var typeR=i.GetType().GetProperty(“类型”);
this.R_STATEFP=typeR.GetValue(i)作为字符串;
var typeL=i.GetType().GetProperty(“类型”);
this.L_STATEFP=typeL.GetValue(i)作为字符串;
}
}
公共课几何
{
公共字符串类型;
公共列表坐标;
[JsonConstructor]
公共几何(JObject i)
{
var typeProp=i.GetType().GetProperty(“类型”);
this.type=typeProp.GetValue(i)作为字符串;
JArray坐标=(Newtonsoft.Json.Linq.JArray)i.GetValue(“坐标”);
this.coordinates=新列表();
foreach(坐标中的JArray c)
{
此.coordinates.Add(新坐标(c));
}
}
}
公共类合作社
{
公共双经度;
公共双纬度;
[JsonConstructor]
公共合作社(JArray a){
this.longitude=(双精度)a[0];
这个纬度=(双)a[1];
}
}
}
另外,打开这样一个大文件的最好方法是什么(假设它有100多个功能),streamreader是最好的方法吗
谢谢您可以大大简化您的设计 如果将类设置为表示数据的普通类:
public class Properties
{
public string Type { get; set; }
[JsonProperty(PropertyName = "R_STATEFP")]
public string RState { get; set; }
[JsonProperty(PropertyName = "L_STATEFP")]
public string LState { get; set; }
}
public class Geometry
{
public string Type { get; set; }
public List<List<double>> Coordinates { get; set; }
}
public class Feature
{
public string Type { get; set; }
public Properties Properties { get; set; }
public Geometry Geometry { get; set; }
}
public class RootObject
{
public string Type { get; set; }
public List<Feature> Features { get; set; }
}
你可以在行动中看到它我认为你应该重新思考你的课程。让它们成为简单的POCO表示,让
jsonvert
完成它的工作。
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);