Cindy Kim
Cindy Kim

Reputation: 11

How can I parse JSON in Windows Phone?

I would like to parse this page

That page has json data like this :

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

I tried below code.(I used JSON.NET) but I was concerend about "List" and I also tried JArray and... o["Lists"]["name"] but I couldn't get a right results. The below code also return null messages. Please help me out.

code

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);

        }

Upvotes: 0

Views: 2489

Answers (1)

Gros Lalo
Gros Lalo

Reputation: 1088

Given it is a list, you should index the elements of the JArray. Here is a sample code to help you out (Notice the [0] => referencing the 1st element of the 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"]);
}

Upvotes: 2

Related Questions