Help us understand the problem. What is going on with this article?

C# + LINQ でJSONのパースをする

More than 5 years have passed since last update.

今回はWindowsPhoneです。

JSON.netを利用します。解析対象はFoursquareのexploreAPI。

レスポンスのJSONの内、Venueのみを抽出します。

結論から言うと、シリアライズした方が楽。

まずは結果を保存するvenue.cs

venue.cs
namespace ConflimJsonParser
{
    public class Venue
    {
        public string name { get; set; }
        public string address { get; set; }
        public string crossStreet { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
        public string cc { get; set; }
        public double lat { get; set; }
        public double lng { get; set; }
        public string id { get; set; }
    }
}

そして、実際に解析を行うMyParser.cs

MyParser.cs
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Diagnostics;

namespace ConflimJsonParser
{
    public class MyParser
    {
        private string json = "省略";

        public void parse()
        {
            var str = JObject.Parse(json).SelectToken("response.groups").ToString();
            var venues = JArray.Parse(str).SelectMany(groups => groups["items"])
                .Select(items => items["venue"]).Select(venue =>
                {
                    var location = venue["location"];
                    return new Venue()
                    {
                        name = (string)venue["name"],
                        id = (string)venue["id"],
                        address = (string)location["address"],
                        crossStreet = (string)location["crossStreet"],
                        city = (string)location["city"],
                        state = (string)location["state"],
                        country = (string)location["country"],
                        cc = (string)location["cc"],
                        lat = (double)location["lat"],
                        lng = (double)location["lng"],
                    };
                });

            foreach (var venue in venues)
            {
                Debug.WriteLine(venue.name);

            }

        }

    }
}

同じ事をAndroidのJsonObjectとかを使ってやろうとすると、
forやifの鬼ネストになるんだろうな。

そういう意味では、すっきりしていてLINQは好きです。

好きだけど、やっぱりパーツの再利用が上手くできないというか何というか。

その辺上手く書けるようになりたいものですね。

numa08
ソースコードカワイイガチ勢
http://numa08.blogspot.jp/
covelline
もっと楽しい明日をつくる
http://covelline.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした