0

顧客データ (名前と電子メール) の JSON 応答を解析し、同じ列見出しを持つ csv ファイルを作成しようとしています。

なんらかの理由で、このコードを実行するたびに、1 つのセルにすべての名のリストが含まれる CSV ファイルを取得します (名前の間に区切りはありません...名前の文字列が互いに追加されているだけです)。姓のためのもの。次のコードには、電子メールの追加は含まれていません (これについては後で考えます)。

コード:

 def self.fetch_emails

    access_token ||= AssistlyArticle.remote_setup
    cust_response = access_token.get("https://blah.desk.com/api/v1/customers.json")
    cust_ids = JSON.parse(cust_response.body)["results"].map{|w| w["customer"]["id"].to_i}

 FasterCSV.open("/Users/default/file.csv", "wb") do |csv|
      # header row
      csv << ["First name", "Last Name"]
      # data rows
      cust_ids.each do |cust_firstname|
        json = JSON.parse(cust_response.body)["results"]
        csv << [json.map{|x| x["customer"]["first_name"]}, json.map{|x| x["customer"]["last_name"]}]
      end 
    end
  end

出力:

First Name        | Last Name
JohnJillJamesBill   SearsStevensSethBing

等々...

望ましい出力:

First Name | Last Name
John       | Sears
Jill       | Stevens
James      | Seth
Bill       | Bing

サンプル JSON:

{
    "page":1,
    "count":20,
    "total":541,
    "results":
    [
        {
            "customer":
            {
                "custom_test":null,
                "addresses":
                [
                    {
                        "address":
                        {
                            "region":"NY",
                            "city":"Commack",
                            "location":"67 Harned Road,
                             Commack,
                             NY 11725,
                             USA",
                            "created_at":"2009-12-22T16:21:23-05:00",
                            "street_2":null,
                            "country":"US",
                            "updated_at":"2009-12-22T16:32:37-05:00",
                            "postalcode":"11725",
                            "street":"67 Harned Road",
                            "lng":"-73.196225",
                            "customer_contact_type":"home",
                            "lat":"40.716894"
                        }
                    }
                ],
                "phones":
                [
                ],
                "last_name":"Suriel",
                "custom_order":"4",
                "first_name":"Jeremy",
                "custom_t2":"",
                "custom_i":"",
                "custom_t3":null,
                "custom_t":"",
                "emails":
                [
                    {
                        "email":
                        {
                            "verified_at":"2009-11-27T21:41:11-05:00",
                            "created_at":"2009-11-27T21:40:55-05:00",
                            "updated_at":"2009-11-27T21:41:11-05:00",
                            "customer_contact_type":"home",
                            "email":"jeremysuriel+twitter@gmail.com"
                        }
                    }
                ],
                "id":8,
                "twitters":
                [
                    {
                        "twitter":
                        {
                            "profile_image_url":"http://a3.twimg.com...",
                            "created_at":"2009-11-25T10:35:56-05:00",
                            "updated_at":"2010-05-29T22:41:55-04:00",
                            "twitter_user_id":12267802,
                            "followers_count":93,
                            "verified":false,
                            "login":"jrmey"
                        }
                    }
                ]
            }
        },
        {
            "customer":
            {
                "custom_test":null,
                "addresses":
                [
                ],
                "phones":
                [
                ],
                "last_name":"",
                "custom_order":null,
                "first_name":"jeremy@example.com",
                "custom_t2":null,
                "custom_i":null,
                "custom_t3":null,
                "custom_t":null,
                "emails":
                [
                    {
                        "email":
                        {
                            "verified_at":null,
                            "created_at":"2009-12-05T20:39:00-05:00",
                            "updated_at":"2009-12-05T20:39:00-05:00",
                            "customer_contact_type":"home",
                            "email":"jeremy@example.com"
                        }
                    }
                ],
                "id":27,
                "twitters":
                [
                    null
                ]
            }
        }
    ]
}

これを可能にするための FasterCSV のより良い使用方法はありますか? << は毎回新しい行に追加されると思いました...しかし、機能していないようです。助けていただければ幸いです。

4

1 に答える 1

1

どういうわけかすべてが絡み合っています.jsonを何度も解析しています(そしてループ内で!)もっと簡単にしましょう:

customers = JSON.parse(data)["results"].map{|x| x['customer']}
customers.each do |c|
  csv << [c['first_name'], c['last_name']]
end

また、「wb」はcsvの間違ったモードです-「w」だけです。

于 2012-09-05T02:26:03.460 に答える