2017-03-19 6 views
0

VK APIから次のリストに国のタイトルを書く必要があります。linkVK APIのリストに各国を書く

public class GettingCountry 
{ 
    public async Task<string> FetchAsync(string url) 
    { 
     string jsonString; 
     using (var httpClient = new System.Net.Http.HttpClient()) 
     { 
      var stream = await httpClient.GetStreamAsync(url); 
      StreamReader reader = new StreamReader(stream); 
      jsonString = reader.ReadToEnd(); 
     } 

     var readJson = JObject.Parse(jsonString); 
     string countryName = readJson["response"]["items"].ToString(); 
     var deserialized = JsonConvert.DeserializeObject<RootObject>(jsonString); 

     return jsonString; 
    } 
} 

public class Item 
{ 
    public int id { get; set; } 
    public string title { get; set; } 
} 

public class Response 
{ 
    public int count { get; set; } 
    public List<Item> items { get; set; } 
} 

public class RootObject 
{ 
    public Response response { get; set; } 
} 

}

私はブレークポイントを入れてstring countryNameこれだけになった:あなたはVKのAPIはあなたの配列を返す見ることができるようにenter image description here

答えて

2

私はいくつかのコードを書かれている

オブジェクト。 jsonStringには完全な応答文字列が含まれています。今すぐ、jsonString ["response"] ["items"]には項目の配列が含まれています。あなたはこのように、配列を解析し、各項目を解析する必要が

まず:

var readJson = JObject.Parse(jsonString); 
JArray countries = JArray.Parse(readJson["response"]["items"]); 

var Response listOfCountries = new Response(); 

foreach (var country in countries) { 
    Item currentCountry = new Item(); 
    currentCountry.id = country.id; 
    currentCountry.title = country.title; 
    listOfCountries.items.Add(currentCountry); 
} 

listOfCountries.count = listOfCountries.items.Count; 

コードの観点から、私は、コードの可読性を向上させるために、変数、クラス、およびタイプに適切な名前を与えることをお勧めしますと、清潔。その上に、私は本当に別の応答クラスを持つのポイントが表示されていません。たとえば、の項目名をに変更し、とすることができます。あなたが持っている必要があるのは、それから国のリストだけです。あなたは、クライアントがすぐに配置される可能性がありますしない場合 - あなたは非同期方法を使用しているので、また、あなたがHttpClientをためを使用して以内に返却の処理をしたいですあなたは非常に奇妙なバグを打ち始めるかもしれません。このように:

public class VkCountry 
{ 
    public int Id { get; } 
    public string Title { get; } 
    public VkCountry(int countryId, string countryTitle) { 
     this.Id = countryId; 
     this.Title = countryTitle; 
    } 
} 

public async Task<List<VkCountry>> FetchAsync(string url) 
{ 
    string jsonString; 
    using (var httpClient = new System.Net.Http.HttpClient()) 
    { 
     var stream = await httpClient.GetStreamAsync(url); 
     StreamReader reader = new StreamReader(stream); 
     jsonString = reader.ReadToEnd(); 

     var listOfCountries = new List<VkCountry>(); 

     var responseCountries = JArray.Parse(JObject.Parse(jsonString)["response"]["items"].ToString()); 

     foreach (var countryInResponse in responseCountries) { 
      var vkCountry = new VkCountry((int)countryInResponse["id"], (string)countryInResponse["title"]); 

      listOfCountries.Add(vkCountry); 
     } 

     return listOfCountries; 
    } 
} 

あなたは、私が不変VkContry実装を行ったことに気づくかもしれません、プロパティは読み取り専用であり、唯一のコンストラクタを使用して設定することができます。比較的静的なサードパーティ製のAPIを使用しているときは、不変のオブジェクトを使用することをお勧めします(アプリケーションロジックによっては国の名前を更新する必要がない限り、国の一覧は間違いありません)。明らかに、NULL可能性チェックと異なる検証を追加することができます。

+1

編集:リアル(とない擬似)なるようにコードを修正 –

関連する問題