草庐IT

map - 带有类型映射的数组

coder 2024-07-10 原文

这是数组

parts:[map[content:Phillip,

This section pertains to terminated employees who are paid out in the year following the termination event.  The way the tax law works, the tax basis for your share distribution will be based on the closing stock price the day preceding notification to the transfer agent.  As such, we will distribute net shares calculating the proper withholding at fair market value the day prior to notifying the transfer agent.  We will be distributing the shares reflected on your 9/30/01 statement (6,606 shares plus cash for fractional shares).  If you would prefer to settle the taxes with a personal check, we can distribute gross shares.  Please let me know you preference.

As you know, we are in the process of transferring recordkeeping services from NTRC to Hewitt.  As such, we have a CPA, Larry Lewis, working with us to audit and set up transition files.  He has become our department expert on the PSA account (much more knowledgeable than myself)  and the various plan provision amendments.  If you would like, we can set up a conference call with you, myself, and Larry to go over the payment methodology.  Please let me know a date and time that is convenient for you.

Thanks,

Renee

 -----Original Message-----
From:   Allen, Phillip K.
Sent:   Thursday, November 01, 2001 8:26 AM
To: Ratcliff, Renee
Subject:

Renee,

Thank you for digging in to the issue of Deferred Phantom Stock Units.  It is clear that the payment will be made in shares.  However, I still don't understand which date will be used to determine the value and calculate how many shares.  The plan document under VII.  Amount of Benefit Payments reads "The value of the shares, and resulting payment amount will be based on the closing price of Enron Corp. common stock on the January 1 before the date of payment, and such payment shall be made in shares of Enron Corp. common stock."  Can you help me interpret this statement and work through the numbers on my account.

Thank you,

 Phillip Allen

 contentType:text/plain]]

我遇到的问题是试图“满足”我已经尝试过类似的东西

t := v["parts"][0].(map[string]interface{})

这并没有像其他一些让我陷入困境的事情那样奏效。

该部分位于另一个映射字符串接口(interface)内。

这是我不断收到的错误

panic: interface conversion: interface is []interface {}, not map[string]interface {}

这是我正在解析的 JSON 对象。

{
    "X-cc": "", 
    "From": "renee.ratcliff@enron.com", 
    "X-Folder": "\\PALLEN (Non-   Privileged)\\Allen, Phillip K.\\Inbox", 
    "Content-Transfer-Encoding": "7bit", 
    "X-bcc": "", "X-Origin": "Allen-P", 
    "To": ["k..allen@enron.com"], 
    "parts": [{
      "content": "Phillip,\r\n\r\nThis section pertains to terminated employees who are paid out in the year following the termination event.  The way the tax law works, the tax basis for your share distribution will be based on the closing stock price the day preceding notification to the transfer agent.  As such, we will distribute net shares calculating the proper withholding at fair market value the day prior to notifying the transfer agent.  We will be distributing the shares reflected on your 9/30/01 statement (6,606 shares plus cash for fractional shares).  If you would prefer to settle the taxes with a personal check, we can distribute gross shares.  Please let me know you preference.\r\n\r\nAs you know, we are in the process of transferring recordkeeping services from NTRC to Hewitt.  As such, we have a CPA, Larry Lewis, working with us to audit and set up transition files.  He has become our department expert on the PSA account (much more knowledgeable than myself)  and the various plan provision amendments.  If you would like, we can set up a conference call with you, myself, and Larry to go over the payment methodology.  Please let me know a date and time that is convenient for you.\r\n\r\nThanks,\r\n\r\nRenee\r\n\r\n -----Original Message-----\r\nFrom: \tAllen, Phillip K.  \r\nSent:\tThursday, November 01, 2001 8:26 AM\r\nTo:\tRatcliff, Renee\r\nSubject:\t\r\n\r\nRenee,\r\n\r\nThank you for digging in to the issue of Deferred Phantom Stock Units.  It is clear that the payment will be made in shares.  However, I still don't understand which date will be used to determine the value and calculate how many shares.  The plan document under VII.  Amount of Benefit Payments reads \"The value of the shares, and resulting payment amount will be based on the closing price of Enron Corp. common stock on the January 1 before the date of payment, and such payment shall be made in shares of Enron Corp. common stock.\"  Can you help me interpret this statement and work through the numbers on my account.\r\n\r\nThank you,\r\n\r\nPhillip Allen\r\n\r\n", 

    "contentType": "text/plain"}], 
    "X-FileName": "PALLEN (Non-Privileged).pst", 
    "Mime-Version": "1.0", 
    "X-From": "Ratcliff, Renee </O=ENRON/OU=NA/CN=RECIPIENTS/CN=RRATCLI>", 
    "Date": {"$date": 1004725111000}, 
    "X-To": "Allen, Phillip K. </O=ENRON/OU=NA/CN=RECIPIENTS/CN=Pallen>", 
    "Message-ID": "<19495537.1075862165839.JavaMail.evans@thyme>", 
    "Content-Type": "text/plain; charset=us-ascii", "Subject": "RE:"
}

这是代码。

http://play.golang.org/p/rJPTjvoM_t

最佳答案

请张贴您尝试过的和您正在尝试做的实际示例。

映射数组和数组映射的行为与任何其他类型一样。

在您的示例中,看起来您想要一个 map[string][]map[string]interface{} 但您有 map[string][]interface{} code>,你的界面内容是一个[]interface{}

这里有一些使用 map /接口(interface)/数组/slice 的例子+你展示的失败。 ( http://play.golang.org/p/gmH_nVPppx )

package main

import "fmt"

type Content struct {
        name string
}

func main() {
        arrayOfMap := [2]map[string]Content{
                {"part1": {name: "Philip"}},
                {"part2": {name: "John"}},
        }
        fmt.Printf("%s\n", arrayOfMap[0]["part1"].name)

        mapOfArray := map[string][2]Content{
                "parts": {
                        {name: "Philip"},
                        {name: "John"},
                },
        }
        fmt.Printf("%s\n", mapOfArray["parts"][0].name)

        mapOfArrayOfMap := map[string][2]map[string]Content{
                "parts": {
                        {"subpart": Content{name: "Philips"}},
                },
        }
        fmt.Printf("%s\n", mapOfArrayOfMap["parts"][0]["subpart"].name)

        mapOfArrayOfInterface := map[string][2]interface{}{
                "parts": {
                        map[string]interface{}{"content": Content{name: "Philips"}},
                },
        }
        fmt.Printf("%s\n", mapOfArrayOfInterface["parts"][0].(map[string]interface{})["content"].(Content).name)

        mapOfArrayOfSliceInterface := map[string][2][]interface{}{
                "parts": {
                        {
                                map[string]interface{}{
                                        "content": Content{name: "Philips"},
                                },
                        },
                },
        }
        fmt.Printf("%s\n", mapOfArrayOfSliceInterface["parts"][0][0].(map[string]interface{})["content"].(Content).name)

        v := map[string][]interface{}{
                "parts": []interface{}{
                        0: []interface{}{nil},
                },
        }
        // This will fail as shown in the given example.
        fmt.Printf("%s\n", v["parts"][0].(map[string]interface{}))
}

关于map - 带有类型映射的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361859/

有关map - 带有类型映射的数组的更多相关文章

  1. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  2. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  3. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  4. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  5. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  6. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

  7. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  8. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  9. ruby - 在 Ruby 中用键盘诅咒数组浏览 - 2

    我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作

  10. ruby - 如何在 Grape 中定义哈希数组? - 2

    我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>

随机推荐