【问题标题】:Array with type map具有类型映射的数组
【发布时间】:2014-08-13 06:04:49
【问题描述】:

所以这里是数组

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{})

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

这些部分在另一个地图字符串接口内。

这是我不断收到的错误

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

【问题讨论】:

  • 这些是 2011 年的安然电子邮件,或者整个丑闻发生时的类似情况。

标签: map interface go


【解决方案1】:

请发布一个实际示例,说明您已尝试过什么以及您正在尝试做什么。

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

在您的示例中,您似乎想要map[string][]map[string]interface{},但您有map[string][]interface{},而您的界面内容是[]interface{}

以下是使用 maps/interface/array/slices + a 您显示的失败的一些示例。 (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{}))
}

【讨论】:

  • 希望这对我有帮助,我只是将其发布在 play.golang play.golang.org/p/rJPTjvoM_t 上面附有 JSON 示例
  • 我冒昧地在不改变逻辑的情况下重写了一点。您正在尝试将 map[string]interface{} 作为切片访问。您应该对 map[string][]interface{} 进行类型断言来执行此操作。 play.golang.org/p/I2rJe22J_l
  • 但是通常,您不想解组到接口。最好了解您的数据并将其解组为已知类型。 play.golang.org/p/F3Y1TG5-XY.
  • 该文件有数百个单独的 json 对象,它们不在一个数组中,这就是为什么我拆分在创建数组的 \n 上读取的完整文件
  • 我也尝试在我的代码中使用上面声明的类型... Content 和 Inner 它返回 {[]}
【解决方案2】:

好吧,在从上面的 cmets 中获取了很多东西之后......我想说@creack 所拥有的最接近能够从多维接口类型中获取“内容”值的东西是下面的链接。

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

【讨论】:

    猜你喜欢
    • 2014-01-20
    • 1970-01-01
    • 2013-03-21
    • 2019-12-04
    • 2018-12-21
    • 2022-11-07
    • 2018-11-16
    • 2018-02-05
    • 2019-02-26
    相关资源
    最近更新 更多