C# 如何使用Newtonsoft转换没有名称的json字符串?

C# 如何使用Newtonsoft转换没有名称的json字符串?,c#,json.net,C#,Json.net,Json字符串 {"geonames":[{"continent":"EU","capital":"Andorra la Vella","languages":"ca","geonameId":3041565,"south":42.42849259876837,"isoAlpha3":"AND","north":42.65604389629997,"fipsCode":"AN","population":"84000","east":1.7865427778319827,"isoNumeric"

Json字符串

{"geonames":[{"continent":"EU","capital":"Andorra la Vella","languages":"ca","geonameId":3041565,"south":42.42849259876837,"isoAlpha3":"AND","north":42.65604389629997,"fipsCode":"AN","population":"84000","east":1.7865427778319827,"isoNumeric":"020","areaInSqKm":"468.0","countryCode":"AD","west":1.4071867141112762,"countryName":"Andorra","continentName":"Europe","currencyCode":"EUR"},{"continent":"AS","capital":"Abu Dhabi","languages":"ar-AE,fa,en,hi,ur","geonameId":290557,"south":22.633329391479492,"isoAlpha3":"ARE","north":26.08415985107422,"fipsCode":"AE","population":"4975593","east":56.38166046142578,"isoNumeric":"784","areaInSqKm":"82880.0","countryCode":"AE","west":51.58332824707031,"countryName":"United Arab Emirates","continentName":"Asia","currencyCode":"AED"},{"continent":"AS","capital":"Kabul","languages":"fa-AF,ps,uz-AF,tk","geonameId":1149361,"south":29.377472,"isoAlpha3":"AFG","north":38.483418,"fipsCode":"AF","population":"29121286","east":74.879448,"isoNumeric":"004","areaInSqKm":"647500.0","countryCode":"AF","west":60.478443,"countryName":"Afghanistan","continentName":"Asia","currencyCode":"AFN"},{"continent":"AF","capital":"Harare","languages":"en-ZW,sn,nr,nd","geonameId":878675,"south":-22.417738,"isoAlpha3":"ZWE","north":-15.608835,"fipsCode":"ZI","population":"13061000","east":33.056305,"isoNumeric":"716","areaInSqKm":"390580.0","countryCode":"ZW","west":25.237028,"countryName":"Zimbabwe","continentName":"Africa","currencyCode":"ZWL"}]}
如何强制Newtonsoft解析数据

例外情况 Newtonsoft.Json.JsonSerializationException:'无法将当前Json数组(例如[1,2,3])反序列化为类型'App.Geonames',因为该类型需要一个Json对象(例如{name:value})才能正确反序列化。 若要修复此错误,请将JSON更改为JSON对象,例如{name:value},或将反序列化类型更改为数组或实现集合接口的类型,例如可以从JSON数组反序列化的ICollection、IList类列表。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化

更新

解决方案

控制器

string GeoNamesCountry = "http://api.geonames.org/countryInfo?username=justlearntutors&type=json";

string GeoNamesCountryResult = "";
using (WebClient webclient = new WebClient())
{
    GeoNamesCountryResult = webclient.DownloadString(GeoNamesCountry);
}

GeonamesObject geonamesObject = Newtonsoft.Json.JsonConvert.DeserializeObject<GeonamesObject>(GeoNamesCountryResult);

请检查这些东西是否有效

GeonamesObject geonamesObject = new GeonamesObject();
geonamesObject.geonames = 
 Newtonsoft.Json.JsonConvert.DeserializeObject<List<Geonames>>(GeoNamesCountryResult);

您可以使用VisualStudio根据json字符串自动生成类。 复制你的json字符串,开始

编辑->粘贴特殊->将JSON粘贴为类注意:JSON字符串必须有效

然后它将为您创建类注意:使用JsonProperty属性,以使用指定名称序列化/取消序列化成员

public class Rootobject
{
    [JsonProperty("geonames")]
    public Geoname[] Geonames { get; set; }
}


public class Geoname
{
    [JsonProperty("continent")]
    public string Continent { get; set; }

    [JsonProperty("capital")]
    public string Capital { get; set; }

    [JsonProperty("languages")]
    public string Languages { get; set; }

    [JsonProperty("geonameId")]
    public int GeonameId { get; set; }

    [JsonProperty("south")]
    public float South { get; set; }

    [JsonProperty("isoAlpha3")]
    public string IsoAlpha3 { get; set; }

    [JsonProperty("north")]
    public float North { get; set; }

    [JsonProperty("fipsCode")]
    public string FipsCode { get; set; }

    [JsonProperty("population")]
    public string Population { get; set; }

    [JsonProperty("east")]
    public float East { get; set; }

    [JsonProperty("isoNumeric")]
    public string IsoNumeric { get; set; }

    [JsonProperty("areaInSqKm")]
    public string AreaInSqKm { get; set; }

    [JsonProperty("countryCode")]
    public string CountryCode { get; set; }

    [JsonProperty("west")]
    public float West { get; set; }

    [JsonProperty("countryName")]
    public string CountryName { get; set; }

    [JsonProperty("continentName")]
    public string ContinentName { get; set; }

    [JsonProperty("currencyCode")]
    public string CurrencyCode { get; set; }

}
然后反序列化它:

var result = JsonConvert.DeserializeObject<Rootobject>(json_string);

多大的样本数据啊。无论如何,你介意分享你的一段代码吗?也许我不完全确定这里的问题是什么,因为我可以使用你的模型和代码成功地反序列化你的json。你能帮我贴一个说明问题的帖子吗?我将所有代码粘贴到LINQPad中,它成功运行,并在geonames数组中生成了一个包含250个GeonamesCountry对象的GeonamesObject。简而言之,您的代码是有效的,问题是什么?非常有用。我可以对xml和csv执行同样的操作吗?xml有一个选项:编辑->粘贴特殊->将xml粘贴为类。还要记住,json或xml字符串应该是有效的。
var result = JsonConvert.DeserializeObject<Rootobject>(json_string);