randomuser.me APIとやりとりするアプリケーションを作成しようとしています これは常にエラーを返します。今回は見つかったコードを使用していますjsonの内容を解析するためにstackoverflowで。ネストされたjson要素を取得するとエラーが返される
public string GetJsonPropertyValue(string json, string query)
{
JToken token = JObject.Parse(json);
foreach (string queryComponent in query.Split('.'))
{
token = token[queryComponent];
}
return token.ToString();
}
string getName()
{
string name = "";
try
{
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("https://randomuser.me/api/");
name = GetJsonPropertyValue(json, "results[0].name.first");
return name;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return name;
}
}
私は本当に正確な問題だかわからないが、私は第二に、インデックスを挿入していない場合、それはSystem.NullReferenceException
EDIT
を返しています: だからここに私のコードのRightNowですGetJsonPropretyValue基メソッドのパラメータ、およびこのようresults.name.first
に挿入これは、このようなエラーを返し:
System.ArgumentException: Accessed JArray values with invalid key value: >"name". Array position index expected.
at Newtonsoft.Json.Linq.JArray.get_Item(Object key)
可能性のある重複した[とNullReferenceExceptionは何ですか?](http://stackoverflow.com/questions/4660142/) –
Edit @DourHighArch – PHPdevpro
生のJSON文字列はどのように見えますか? –