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"})に変更するか、デシリアライズされた型を配列またはListのようなコレクションインターフェイス(ICollection、IList)を実装する型に変更しますJSON配列からデシリアライズされます。 JsonArrayAttributeを型に追加して、JSON配列から逆シリアル化することもできます。
更新しました
Solution
コントローラ
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);
モデル
public class GeonamesObject
{
public GeonamesCountry[] geonames { get; set; }
}
public class GeonamesCountry
{
public string countryCode { get; set; }
public string countryName { get; set; }
public string isoNumeric { get; set; }
public string isoAlpha3 { get; set; }
public string fipsCode { get; set; }
public string continent { get; set; }
public string continentName { get; set; }
public string capital { get; set; }
public string areaInSqKm { get; set; }
public string population { get; set; }
public string currencyCode { get; set; }
public string languages { get; set; }
public int geonameId { get; set; }
public string west { get; set; }
public string north { get; set; }
public string east { get; set; }
public string south { get; set; }
public string postalCodeFormat { get; set; }
}
回答 2 件
このことを確認してください
GeonamesObject geonamesObject = new GeonamesObject(); geonamesObject.geonames = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Geonames>>(GeoNamesCountryResult);
関連記事
- replace()関数を使用せずに文字列内の部分文字列を大文字に変換するにはどうすればよいですか?
- 二重引用符で囲まれた要素のないリストの文字列表現を実際のリストに変換するにはどうすればよいですか?
- 文字列をブール値に変換する最良の方法は?
- 整数を変換して文字列を出力する最良の方法
- 日付形式の複数の文字列をタイムスタンプ形式に変換する方法
- 数値の桁を変更せずに10進数から8進数に変換するにはどうすればよいですか? (python)
- Python(ジェネレータ式)で文字列をネストされた辞書に変換するにはどうすればよいですか?
- タイムスタンプ文字列を「%d-%m-%Y」の形式に変換します
- splitメソッドを使用せずに文字列内の最長の単語を見つける方法
- 文字列をルーンに変換する方法は?
関連した質問
- NewtonsoftJson:JPropertyのコンテンツ値としてDictionary を使用します
- [JsonConstructor]があるかのように、Jsonnetに「プライマリ」コンストラクターを使用してC#9レコードタイプを逆シリアル化させることはできますか?
- プライベートリストを使用したカスタムコレクションのシリアル化の問題
- (解決済み)NewtonsoftJsonReferenceLoopHandlingIgnore deserialization format NET Core 31
- 複雑なJsonからC#オブジェクトへのクラスによる逆シリアル化
- 配列のIEnumerableへのJsonDeSerializationの失敗
- 暗黙的な変換で文字列を型にキャストできません
- NewtonsoftJSONでエンコードおよびデコードするプロパティとフィールドを指定します
- NewtonsoftJson SystemInvalidOperationException:同期操作は許可されていません
- データメンバーとしてのみフィールド名を使用してjsonをシリアル化する方法は?
Visual Studioを使用して、json文字列に基づいてクラスを自動生成できます。 JSON文字列をコピーして、
その後、クラスが作成されます(注:
JsonProperty
を使用します 属性、指定された名前を持つメンバーのシリアル化/非シリアル化)それをデシリアライズします: