2016-11-26 2 views
1

Stackoverflow:tableaux unmarshalling dans la structure JSON

J'ai du mal à désorganiser ce que je ne considère pas une réponse JSON particulièrement complexe dans le réseau GO. (que je suis assez nouveau à). Exemple ci-dessous:

{ "eventId": "tevtNKIsHrFQTyyMeYDMc5jgQ1459184873000", 
    "sessionId": "1016Q-vnpnlQwCiLiyH7e_cNg", 
    "targets": 
    [ { "id": "00u34k73otQGIAFUALPR", "displayName": "okta admin", "login":  "[email protected]", "objectType": "User" } ] } 

J'ai essayé représentant cela comme un tableau de struct, mais il ne semble jamais se connecter.

J'ai mis mon code sur le terrain de jeu GO Lang, si quelqu'un peut jeter un oeil je serais très reconnaissant.

https://play.golang.org/p/TVYeYe7e_I

Répondre

1

Pour les gros documents JSON je vous recommande d'utiliser cet outil: https://mholt.github.io/json-to-go/

Vous obtiendrez quelque chose comme:

type AutoGenerated struct { 
    EventID string `json:"eventId"` 
    SessionID string `json:"sessionId"` 
    RequestID string `json:"requestId"` 
    Published time.Time `json:"published"` 
    Action struct { 
     Message string `json:"message"` 
     Categories []string `json:"categories"` 
     ObjectType string `json:"objectType"` 
     RequestURI string `json:"requestUri"` 
    } `json:"action"` 
    Actors []struct { 
     ID   string `json:"id"` 
     DisplayName string `json:"displayName"` 
     Login  string `json:"login,omitempty"` 
     ObjectType string `json:"objectType"` 
     IPAddress string `json:"ipAddress,omitempty"` 
    } `json:"actors"` 
    Targets []struct { 
     ID   string `json:"id"` 
     DisplayName string `json:"displayName"` 
     Login  string `json:"login"` 
     ObjectType string `json:"objectType"` 
    } `json:"targets"` 
} 

Exemple complet: https://play.golang.org/p/Q8PwwtS_QZ

vous pouvez également commencez toujours par map[string]interface{} au lieu d'un struct.

+1

Ce JSON GO Struct est incroyable! Merci – user3888307

0

Veuillez changer la structure comme ci-dessous.

type zMessage struct { 
     Message string `json:"message"` 
    } 

    type zTargets struct { 
     Idtarget string `json:"id"` 
    } 


    var val struct {  
     Targets [] zTargets `json:"targets"`   

    } 

L'ID de la cible sera imprimé en cours de codage.

{[{00u34k73otQGIAFUALPR}]} 
0

Vous devez modifier vos struct et utiliser un code comme dans cet exemple:

package main 

import (
    "encoding/json" 
    "fmt" 
) 

type MyJSON struct { 
    EventID string  `json:"eventID"` 
    SessionID string  `json:"sessionID"` 
    Targets []MyTargets `json:"targets"` 
} 

type MyTargets struct { 
    Id   string `json:"id"` 
    DisplayName string `json:"displayName"` 
    Login  string `json:"login"` 
    ObjectType string `json:"objectType"` 
} 

func main() { 
    myJson := []byte(`{ 
    "eventId": "tevtNKIsHrFQTyyMeYDMc5jgQ1459184873000", 
    "sessionId": "1016Q-vnpnlQwCiLiyH7e_cNg", 
    "targets": [ 
     { 
      "id": "00u34k73otQGIAFUALPR", 
       "displayName": "okta admin", 
       "login":"[email protected]", 
       "objectType": "User" 
     } 
    ] 
}`) 

    myStruct := MyJSON{} 
    json.Unmarshal(myJson, &myStruct) 
    fmt.Printf(` 
    eventId: %s 
    sessionID: %s 
    targets.id: %s 
    targets.displayName: %s 
    targets.login: %s 
    targets.objectType: %s 

`, myStruct.EventID, myStruct.SessionID, 
    myStruct.Targets[0].Id, 
    myStruct.Targets[0].DisplayName, 
    myStruct.Targets[0].Login, 
    myStruct.Targets[0].ObjectType) 
} 

Sortie:

eventId: tevtNKIsHrFQTyyMeYDMc5jgQ1459184873000 
sessionID: 1016Q-vnpnlQwCiLiyH7e_cNg 
targets.id: 00u34k73otQGIAFUALPR 
targets.displayName: okta admin 
targets.login: [email protected] 
targets.objectType: User 

vous pouvez également vérifier ce code dans https://play.golang.org/p/as9QJS4Cav