json 反序列化为 class 并在 C# 中访问结果
json deserialize into class and access results in c#
我正在将 json 反序列化为 class,它似乎有效,但我似乎无法弄清楚如何访问结果。我正在实例化 class,然后尝试将反序列化的 json 放入其中,但是 class 容器无法访问。我不确定如何使用各个容器。
这是class
public class geocodes
{
public Boolean outcome { get; set; }
public double xcord { get; set; }
public double ycord { get; set; }
}
class jsonresult
{
public class Rootobject
{
public Spatialreference spatialReference { get; set; }
public Candidate[] candidates { get; set; }
}
public class Spatialreference
{
public int wkid { get; set; }
public int latestWkid { get; set; }
}
public class Candidate
{
public string address { get; set; }
public Location location { get; set; }
public int score { get; set; }
public Attributes attributes { get; set; }
public Extent extent { get; set; }
}
public class Location
{
public float x { get; set; }
public float y { get; set; }
}
public class Attributes
{
}
public class Extent
{
public float xmin { get; set; }
public float ymin { get; set; }
public float xmax { get; set; }
public float ymax { get; set; }
}
}
这是我用
填充 class 的代码
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
jsonresult results = System.Text.Json.JsonSerializer.Deserialize<jsonresult>(responseString);
...
}
我想通过每个根容器甚至空间参考做一个 foreach(因为它通常 return 不止一个)如果我只是拉出那部分 json 变成了 class,但我不知道怎么弄。
这里是 json:
{
"spatialReference": {
"wkid": 2253,
"latestWkid": 2253
},
"candidates": [
{
"address": "816 Example ST",
"location": {
"x": 13288754.574891519,
"y": 288356.77197193989
},
"score": 100,
"attributes": {
},
"extent": {
"xmin": 13288098.406912517,
"ymin": 287700.60399293725,
"xmax": 13289410.742870521,
"ymax": 289012.93995094253
}
}
]
}
问题的根本原因是额外的(不必要的)层:jsonresult
。
jsonresult
没有任何 属性 这就是为什么 System.Text.Json 无法正确地将数据反序列化到其中的原因。换句话说,在 JsonSerializer
通过调用其无参数构造函数创建 jsonresult
class 的新实例后,它无法将读取值分配给其任何 public 属性。
如果您删除 class 周围的 jsonresult
并在 Deserialize
期间使用 RootObject
然后在 JsonSerializer
创建一个 RootObject
实例之后它可以使用反射(和基于名称的匹配)用数据填充它的属性。
我正在将 json 反序列化为 class,它似乎有效,但我似乎无法弄清楚如何访问结果。我正在实例化 class,然后尝试将反序列化的 json 放入其中,但是 class 容器无法访问。我不确定如何使用各个容器。
这是class
public class geocodes
{
public Boolean outcome { get; set; }
public double xcord { get; set; }
public double ycord { get; set; }
}
class jsonresult
{
public class Rootobject
{
public Spatialreference spatialReference { get; set; }
public Candidate[] candidates { get; set; }
}
public class Spatialreference
{
public int wkid { get; set; }
public int latestWkid { get; set; }
}
public class Candidate
{
public string address { get; set; }
public Location location { get; set; }
public int score { get; set; }
public Attributes attributes { get; set; }
public Extent extent { get; set; }
}
public class Location
{
public float x { get; set; }
public float y { get; set; }
}
public class Attributes
{
}
public class Extent
{
public float xmin { get; set; }
public float ymin { get; set; }
public float xmax { get; set; }
public float ymax { get; set; }
}
}
这是我用
填充 class 的代码using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
jsonresult results = System.Text.Json.JsonSerializer.Deserialize<jsonresult>(responseString);
...
}
我想通过每个根容器甚至空间参考做一个 foreach(因为它通常 return 不止一个)如果我只是拉出那部分 json 变成了 class,但我不知道怎么弄。
这里是 json:
{
"spatialReference": {
"wkid": 2253,
"latestWkid": 2253
},
"candidates": [
{
"address": "816 Example ST",
"location": {
"x": 13288754.574891519,
"y": 288356.77197193989
},
"score": 100,
"attributes": {
},
"extent": {
"xmin": 13288098.406912517,
"ymin": 287700.60399293725,
"xmax": 13289410.742870521,
"ymax": 289012.93995094253
}
}
]
}
问题的根本原因是额外的(不必要的)层:jsonresult
。
jsonresult
没有任何 属性 这就是为什么 System.Text.Json 无法正确地将数据反序列化到其中的原因。换句话说,在 JsonSerializer
通过调用其无参数构造函数创建 jsonresult
class 的新实例后,它无法将读取值分配给其任何 public 属性。
如果您删除 class 周围的 jsonresult
并在 Deserialize
期间使用 RootObject
然后在 JsonSerializer
创建一个 RootObject
实例之后它可以使用反射(和基于名称的匹配)用数据填充它的属性。