gpt4 book ai didi

c# - 如何在 Windows Phone 7 中为 JSON Web 服务结果创建类?

转载 作者:太空宇宙 更新时间:2023-11-03 19:31:38 24 4
gpt4 key购买 nike

如何为 JSON Web 服务结果创建类?这是我从 google api 获得的 JSON 结果。那么,如何为这个 JSON 对象创建类和反序列化。 alt text

我创建了一个类来反序列化这个 JSON 对象,

string mapdetail = e.Result;
var djosn = new DataContractJsonSerializer(typeof(mapResult));
var mstr = new MemoryStream(Encoding.UTF8.GetBytes(mapdetail));
mapResult reslt = (mapResult)djosn.ReadObject(mstr);

map 结果类:

[DataContract]
public class mapResult
{
[DataMember]
public string status
{
get;
set;
}
[DataMember]
public IList<Resultdetail> result
{
get;
set;
}
}

所以我创建了结果详细信息和其他列表:

[DataContract]
public class Resultdetail
{
[DataMember]
public List<string> types
{
get;
set;
}
[DataMember]
public string formatted_address
{
get;
set;
}
[DataMember]
public List<object> address_components
{
get;
set;
}
[DataMember]
public List<Geometry> geometry
{
get;
set;
}

}

[DataContract]
public class Geometry
{
[DataMember]
public List<GeoLocation> location
{
get;
set;
}
[DataMember]
public string location_type
{
get;
set;
}
[DataMember]
public List<object> viewport
{
get;
set;
}
[DataMember]
public List<object> bounds
{
get;
set;
}

}

[DataContract]
public class GeoLocation
{
[DataMember]
public double lat
{
get;
set;
}
[DataMember]
public double lng
{
get;
set;
}

}

现在我得到的是 Null 引用;

alt text

最佳答案

您的 DatContract 类存在一些问题。

我在下面更正了这些内容,注释掉了原始行并添加了关于具体错误的评论。

[DataContract]
public class mapResult
{
[DataMember]
public string status { get; set; }
[DataMember]
//public IList<Resultdetail> result { get; set; }
// Misspelt property name and use of interface rather than concrete type
public List<Resultdetail> results { get; set; }
}

[DataContract]
public class Resultdetail
{
[DataMember]
public List<string> types { get; set; }
[DataMember]
public string formatted_address { get; set; }
[DataMember]
public List<object> address_components { get; set; }
[DataMember]
//public List<Geometry> geometry { get; set; }
// Json does not contain an array/list of these
public Geometry geometry { get; set; }
}

[DataContract]
public class Geometry
{
[DataMember]
//public List<GeoLocation> location { get; set; }
// Json does not contain an array/list of these
public GeoLocation location { get; set; }
[DataMember]
public string location_type { get; set; }
[DataMember]
// public List<object> viewport { get; set; }
// Json does not contain an array/list of these
public object viewport { get; set; }
[DataMember]
//public List<object> bounds { get; set; }
// Json does not contain an array/list of these
public object bounds { get; set; }
}

下面的代码表明这是可行的:

var jsonStr = "{\"status\": \"OK\", \"results\": [ { \"types\": [ \"route\" ], \"formatted_address\": \"7th Main Rd, Koramangala, sengaluru, Karnataka 560034, India\", \"address_components\": [ { \"long_name\": \"7th Main Rd\", \"short_name\": \"7th Main Rd\", \"types\": [ \"route\" ] }, { \"long_name\": \"Koramangala\", \"short_name\": \"Koramangala\", \"types\": [ \"sublocality\", \"political\" ] }, { \"long_name\": \"Bengaluru\", \"short_name\": \"Bengaluru\", \"types\": [ \"locality\", \"political\" ] }, { \"long_name\": \"sengaluru\", \"short_name\": \"sengaluru\", \"types\": [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\": \"Karnataka\", \"short_name\": \"Karnataka\", \"types\": [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\": \"India\", \"short_name\": \"IN\", \"types\": [ \"country\", \"political\" ] }, { \"long_name\": \"560034\", \"short_name\": \"560034\", \"types\": [ \"postal_code\" ] }],\"geometry\": { \"location\":{ \"lat\": 12.9259085, \"lng\": 77.6334715 }, \"location_type\": \"GEOMETRIC_CENTER\", \"viewport\": { \"southwest\": { \"lat\": 12.9227118, \"lng\": 77.6301852 }, \"northeast\": { \"lat\": 12.929007, \"lng\": 77.6364805}}, \"bounds\": { \"southwest\": { \"lat\": 12.9247615, \"lng\": 77.6330486 },\"northeast\": { \"lat\": 12.9269574, \"lng\": 77.6336171 }}}}]}";

// If using json.net (http://json.codeplex.com/)
var des = JsonConvert.DeserializeObject<mapResult>(jsonStr);

// If using System.Runtime.Serialization.Json
var djosn = new DataContractJsonSerializer(typeof(mapResult));
var mstr = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
des = (mapResult)djosn.ReadObject(mstr);

var latitude = des.results[0].geometry.location.lat;
var longitude = des.results[0].geometry.location.lng;

请注意,我的代码仅使用您的示例 json 对象进行了测试,不能保证适用于网络服务返回的所有内容。您还对 object 进行了很多反序列化,如果您想要的不仅仅是纬度和经度,使用起来可能会很尴尬。

关于c# - 如何在 Windows Phone 7 中为 JSON Web 服务结果创建类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4518195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com