我不明白您希望将 html_attributions 和 next_page_token 分开的部分。
仅使用您需要的任何属性反序列化响应还不够吗?
例如,您可以反序列化响应以仅检索您想要的值;
// I represent the wrapper result
class Result
{
public List<string> html_attributions { get; set; }
public string next_page_token { get; set; }
public List<ResultItem> results { get; set; }
}
// I represent a result item
class ResultItem
{
public string id { get; set; }
public string name { get; set; }
}
// the actual deserialization
Result Deserialize(string json)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize(json, typeof(Result));
}
编辑:
您的反序列化不返回字符串数组的原因是因为您检索的响应实际上是一个对象而不是数组,但是该对象中名为 results 的参数是一个数组。为了让您反序列化更多属性,您必须在“ResultItem”类中定义它们,抱歉我在这里命名不佳。
例如,如果您还希望检索每个结果的图标属性,则必须添加一个名为“icon”的字符串类型的属性。
同时属性“photos”是一个数组,为了反序列化它,您必须创建另一个类并添加该新创建类的列表/数组类型的属性,并且它必须命名为“photos”除非您使用不同的序列化程序或使用 DataContract 和 DataMember 属性(使用 Name 属性进行字段映射)。
// the representation of a photo within a result item
class Photo
{
public int height { get; set; }
public List<string> html_attributions { get; set; }
public string photo_reference { get; set; }
public int width { get; set; }
}
// I represent a result item
class ResultItem
{
public string id { get; set; }
public string name { get; set; }
// the added icon
public string icon { get; set; }
// the added photos collection, could also be an array
public List<Photo> photos { get; set; }
}
只需查看 JSON 结果,找出您可能想要添加的其他属性,例如“scope”属性是一个字符串,而“price_level”是一个整数。
如果我正确理解您的评论,您只对实际结果感兴趣,您仍然需要使用其包装器正确反序列化响应。
// the actual deserialization
List<ResultItem> Deserialize(string json)
{
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize(json, typeof(Result));
return result.results;
}
编辑2:
如果你真的想要一个 string[] 作为结果,你可以简单地使用 System.Linq 使用上面的代码。
string[] stringArray = result.results.Select(r => string.Format("id:{0} - name:{1}", r.id, r.name)).ToArray();
编辑3:
除了使用 JavascriptSerializer,您还可以使用 Newtonsoft.Json.Linq 库中的 JObject 功能。
var jsonObject = JObject.Parse(json);
string[] results = jsonObject.SelectTokens("results").Select(r => r.ToString()).ToArray();
这将为您提供一个字符串数组,其中数组中的每个值都是每个结果的实际 json 字符串。
如果您只想查询坐标:
var jsonObject = JObject.Parse(json);
var coordinates = jsonObject["results"]
.Select(x => x.SelectToken("geometry").SelectToken("location"))
.Select(x => string.Format("{0},{1}", (string)x.SelectToken("lat"), (string)x.SelectToken("lng")))
.ToArray();
这会给你一个坐标数组,例如:
[
"-33.867217,151.195939",
"-33.866786,151.195633",
...
]
无论您选择哪种方法,您都可以使用 Newtonsoft 或 .net 序列化程序实现相同的结果,而 Newtonsoft 方法允许您在不创建强类型进行反序列化的情况下进行查询。