내 JSON은 다음과 같습니다.
string url = "https://kgsearch.googleapis.com/v1/entities:search?languages=tr&query=Necati+Şaşmaz&limit=1&key=AIzaSyB_w5S8Bv3P4zn7pqu6if49_4bHx9YMmSY";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
string jsonVerisi = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
//jsonVerisi adlı değişkene elde ettiği veriyi atıyoruz.
jsonVerisi = reader.ReadToEnd();
}
json으로 이미지 선택 위치를 어떻게 얻습니까?
이를 달성하는 두 가지 방법이 있습니다.
참고 : 이러한 클래스는 json2csharp를 사용하여 생성됩니다 .
public class Context {
[JsonProperty("@vocab")] //This is to map the property name with @symbol in JSON content to this class property
public string vocab { get; set; }
public string goog { get; set; }
public string EntitySearchResult { get; set; }
public string detailedDescription { get; set; }
public string resultScore { get; set; }
public string kg { get; set; }
}
public class DetailedDescription {
public string articleBody { get; set; }
public string url { get; set; }
public string license { get; set; }
}
public class Result {
[JsonProperty("@id")]
public string id { get; set; }
public string name { get; set; }
[JsonProperty("@type")]
public List<string> type { get; set; }
public string description { get; set; }
public DetailedDescription detailedDescription { get; set; }
}
public class ItemListElement {
[JsonProperty("@type")]
public string type { get; set; }
public Result result { get; set; }
public double resultScore { get; set; }
}
public class SearchResult {
[JsonProperty("@context")]
public Context context { get; set; }
[JsonProperty("@type")]
public string type { get; set; }
public List<ItemListElement> itemListElement { get; set; }
}
다음과 같이 Newtonsoft Json.NET에서 JsonConvert를jsonVerisi
사용 하여 값을 deserialize하십시오.
using(HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
using(StreamReader reader = new StreamReader(response.GetResponseStream())) {
//jsonVerisi adlı değişkene elde ettiği veriyi atıyoruz.
jsonVerisi = reader.ReadToEnd();
//Deserialize
var searchResult = JsonConvert.DeserializeObject<SearchResult>(jsonVerisi);
//Now you could get the needed value as below
if(searchResult != null) {
foreach(var item in searchResult.itemListElement) {
var resultTypes = item.result != null ? item.result.type : null;
//Iterate through resultTypes to get the list of values
//e.g. for person
//var personType = resultTypes[1];
}
}
}
}
대신에 새로운 클래스를 만드는, 유형 사용 Newtonsoft Json.NET하는 jobject (유사한 요소 액세스 제공 Dictionary<string, object>
과를 JSON key or property name
) 이하로 JSON 컨텐츠를 직렬화.
using(HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
using(StreamReader reader = new StreamReader(response.GetResponseStream())) {
//jsonVerisi adlı değişkene elde ettiği veriyi atıyoruz.
jsonVerisi = reader.ReadToEnd();
var searchResult = JsonConvert.DeserializeObject<JObject>(jsonVerisi);
if(searchResult != null) {
var itemListElement = searchResult["itemListElement"];
if(itemListElement != null) {
foreach(var item in itemListElement.ToList()) {
var result = item["result"];
var resultTypes = result != null && result["@type"].HasValues ? result["@type"].Values<string>().ToList() : null;
//Iterate through resultTypes to get the list of values
//e.g. for person
//var personType = resultTypes[1];
}
}
}
}
}
이것이 귀하의 경우에 도움이되기를 바랍니다!
팁 : HTTP 요청을 수행하는 데 유용한 많은 방법이있는 HttpClient 유형 을 사용하는 것이 좋습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다