如何解析Windows Phone中的JSON?

时间:2012-09-23 08:22:17

标签: json windows-phone-7 json.net

我想解析this page

该页面包含这样的json数据:

{"List":[{"num":"1","name":"hello","ox_score":"30","between_score":"30","order_score":"30","total_score":"90"}]}

我尝试了下面的代码。(我使用JSON.NET)但我对“List”感到失望,我也试过JArray和... o [“Lists”] [“name”]但我不能得到一个正确的结果。以下代码也返回空消息。 请帮帮我。

public void connection()
        {
            string uriString = "http://kah601.cafe24.com/jp_mango_loadboard.php";

            WebClient wc = new WebClient();
            wc.Headers["Accept"] = "application/json";


            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(uriString));


        }
        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

            JObject o = JObject.Parse(e.Result);


            String name = (string) o["name"];
            String ox_score = (string) o["ox_score"];
            String between_score = (string) o["between_score"];
            String order_score = (string) o["order_score"];
            String total_score = (string) o["total_score"];

            String rank_result = name + ox_score + between_score + order_score + total_score;

            MessageBox.Show(rank_result);

        }

1 个答案:

答案 0 :(得分:2)

鉴于它是一个列表,您应该索引JArray的元素。这是一个帮助你的示例代码(注意[0] =>引用JArray的第一个元素):

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{

    JObject o = JObject.Parse(e.Result);
    JArray a = (JArray)o["List"];

    Debug.WriteLine("{0}", (String)a[0]["name"]);
}