解码数组时无法将数组解组为Go结构

时间:2018-05-06 10:32:04

标签: json go unmarshalling

我试图将JSON字符串正确解码为对象。

我定义了以下结构:

type AjaxModelsList struct {
    Id float64 `json:"Id"`
    Name string `json:"Name"`
    CarId float64 `json:"CarId"`
    EngName string `json:"EngName"`
}

type AjaxModelsData struct {
    ModelList []AjaxModelsList `json:"ModelList"`
}

type AjaxModels struct {
    Status bool `json:"status"`
    Data map[string]AjaxModelsData `json:"data"`

}

已定义的对象是

{
 "status": true,
 "data": {
 "ModelList": [{
   "Id": 1,
   "Name": "foo",
   "CarId": 1,
   "EngName": "bar"
  }]
 }
}

我使用以下代码解组:

c :=AjaxModels{}
if err := json.Unmarshal(body_byte,&c); err != nil {
    log.Fatalf("an error occured: %v",err)
}

我得到以下输出:

an error occured: json: cannot unmarshal array into Go struct field AjaxModels.data of type main.AjaxModelsData

因为我使用[]AjaxModelsList它是一片,所以我不应该得到这个错误。我可能错过了什么,但是什么?

1 个答案:

答案 0 :(得分:2)

18.04-61 8341.339: com.yourkit.k.jm: LC@7fc6aab9{localhost:59131,pid=23651}: com.yourkit.b.bb: Bad stream from profiled application Technical detail: EOF at com.yourkit.b.o.a(a:211) at com.yourkit.b.o.b(a:53) at com.yourkit.b.q.a(a:3385) at com.yourkit.b.q.updateTables(a:320) at com.yourkit.api.ControllerImpl$31.perform(a:12) at com.yourkit.api.ControllerImpl.makeRequest(a:18) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.updateDatabaseTables(a:200) at com.yourkit.k.jd.b(a:119) at com.yourkit.k.jd.b(a:286) at com.yourkit.k.jm.a(a:150) at com.yourkit.api.ControllerImpl$30.perform(a:3) at com.yourkit.api.ControllerImpl.makeRequest(a:163) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.runBatch(a:544) at com.yourkit.k.jm.run(a:150) 18.04-61 8341.339: com.yourkit.k.jm: LC@924ccd1{localhost:59570,pid=23851}: com.yourkit.b.bb: Bad stream from profiled application Technical detail: EOF at com.yourkit.b.o.a(a:211) at com.yourkit.b.o.b(a:60) at com.yourkit.b.q.a(a:1275) at com.yourkit.b.q.updateTables(a:320) at com.yourkit.api.ControllerImpl$31.perform(a:12) at com.yourkit.api.ControllerImpl.makeRequest(a:18) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.updateDatabaseTables(a:200) at com.yourkit.k.jd.b(a:119) at com.yourkit.k.jd.b(a:286) at com.yourkit.k.jm.a(a:150) at com.yourkit.api.ControllerImpl$30.perform(a:3) at com.yourkit.api.ControllerImpl.makeRequest(a:163) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.runBatch(a:544) at com.yourkit.k.jm.run(a:150) 18.04-61 8341.339: com.yourkit.k.jm: LC@1fe95a4b{localhost:59991,pid=23983}: com.yourkit.b.bb: Bad stream from profiled application Technical detail: EOF at com.yourkit.b.o.a(a:211) at com.yourkit.b.o.b(a:74) at com.yourkit.b.q.a(a:1854) at com.yourkit.b.q.updateTables(a:320) at com.yourkit.api.ControllerImpl$31.perform(a:12) at com.yourkit.api.ControllerImpl.makeRequest(a:18) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.updateDatabaseTables(a:200) at com.yourkit.k.jd.b(a:119) at com.yourkit.k.jd.b(a:286) at com.yourkit.k.jm.a(a:150) at com.yourkit.api.ControllerImpl$30.perform(a:3) at com.yourkit.api.ControllerImpl.makeRequest(a:163) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.runBatch(a:544) at com.yourkit.k.jm.run(a:150) 18.04-61 8344.342: com.yourkit.k.jm: LC@1fe95a4b{localhost:59991,pid=23983}: com.yourkit.util.bs: There's no application running at localhost with profiler agent configured to listen on port 59991 at com.yourkit.api.ControllerImpl.transformException(a:273) at com.yourkit.api.ControllerImpl.makeRequest(a:293) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.runBatch(a:544) at com.yourkit.k.jm.run(a:150) 18.04-61 8346.665: com.yourkit.b.bh: LC@74c29709{localhost:60361,pid=23651}: created; local=false 18.04-61 8347.545: com.yourkit.b.q: snapshot_outer: starting: null 18.04-61 8347.545: com.yourkit.k.jm: LC@1fe95a4b{localhost:59991,pid=23983}: com.yourkit.util.bs: There's no application running at localhost with profiler agent configured to listen on port 59991 at com.yourkit.api.ControllerImpl.transformException(a:273) at com.yourkit.api.ControllerImpl.makeRequest(a:293) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.runBatch(a:544) at com.yourkit.k.jm.run(a:150) 18.04-61 8347.545: com.yourkit.k.jm: LC@1fe95a4b{localhost:59991,pid=23983}: disconnected 18.04-61 8347.545: com.yourkit.b.q: controller telemetry limit: 3600 18.04-61 8347.545: com.yourkit.b.q: snapshot_outer: done in 0 sec: null 18.04-61 8379.660: com.yourkit.k.my: error: com.yourkit.util.bs: There's no application running at localhost with profiler agent configured to listen on port 60361 or profiler agent is incompatible with current version of profiler at com.yourkit.api.ControllerImpl.transformException(a:278) at com.yourkit.api.ControllerImpl.makeRequest(a:293) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.startCpuProfiling(a:154) at com.yourkit.k.kt.a(a:92) at com.yourkit.k.jo.b(a:60) at com.yourkit.k.d.al.a(a:183) at com.yourkit.k.dz.run(a:16) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.yourkit.b.bj.read(a:21) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at java.io.DataInputStream.readFully(DataInputStream.java:195) at java.io.DataInputStream.readLong(DataInputStream.java:416) at com.yourkit.runtime.Packet.readHeader(a:3) at com.yourkit.runtime.Packet.readHeader(a:51) at com.yourkit.runtime.Packet.readHeaderWithStandardResponse(a:17) at com.yourkit.api.ControllerImpl.readHeaderWithStandardResponse(a:10) at com.yourkit.api.ControllerImpl.access$100(a:558) at com.yourkit.api.ControllerImpl$3.perform(a:8) at com.yourkit.api.ControllerImpl.makeRequest(a:163) ... 6 more 18.04-61 9097.534: com.yourkit.k.my: error: com.yourkit.util.bs: There's no application running at localhost with profiler agent configured to listen on port 60361 or profiler agent is incompatible with current version of profiler at com.yourkit.api.ControllerImpl.transformException(a:278) at com.yourkit.api.ControllerImpl.makeRequest(a:293) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.startCpuProfiling(a:154) at com.yourkit.k.kt.a(a:92) at com.yourkit.k.jo.b(a:60) at com.yourkit.k.d.al.a(a:183) at com.yourkit.k.dz.run(a:16) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.yourkit.b.bj.read(a:21) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at java.io.DataInputStream.readFully(DataInputStream.java:195) at java.io.DataInputStream.readLong(DataInputStream.java:416) at com.yourkit.runtime.Packet.readHeader(a:3) at com.yourkit.runtime.Packet.readHeader(a:51) at com.yourkit.runtime.Packet.readHeaderWithStandardResponse(a:17) at com.yourkit.api.ControllerImpl.readHeaderWithStandardResponse(a:10) at com.yourkit.api.ControllerImpl.access$100(a:558) at com.yourkit.api.ControllerImpl$3.perform(a:8) at com.yourkit.api.ControllerImpl.makeRequest(a:163) ... 6 more 18.04-61 9191.727: com.yourkit.k.jm: LC@74c29709{localhost:60361,pid=23651}: com.yourkit.util.bs: There's no application running at localhost with profiler agent configured to listen on port 60361 or profiler agent is incompatible with current version of profiler at com.yourkit.api.ControllerImpl.transformException(a:278) at com.yourkit.api.ControllerImpl.makeRequest(a:293) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.getStatus(a:308) at com.yourkit.api.ControllerImpl.cacheStatuses(a:175) at com.yourkit.k.jd.b(a:130) at com.yourkit.k.jd.b(a:286) at com.yourkit.k.jm.a(a:150) at com.yourkit.api.ControllerImpl$30.perform(a:3) at com.yourkit.api.ControllerImpl.makeRequest(a:163) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.runBatch(a:544) at com.yourkit.k.jm.run(a:150) Caused by: java.io.EOFException at java.io.DataInputStream.readFully(DataInputStream.java:197) at java.io.DataInputStream.readLong(DataInputStream.java:416) at com.yourkit.runtime.Packet.readHeader(a:3) at com.yourkit.runtime.Packet.readHeader(a:51) at com.yourkit.runtime.Packet.readHeaderWithStandardResponse(a:17) at com.yourkit.api.ControllerImpl.readHeaderWithStandardResponse(a:10) at com.yourkit.api.ControllerImpl.access$100(a:558) at com.yourkit.api.ControllerImpl$2.perform(a:5) at com.yourkit.api.ControllerImpl.makeRequest(a:18) ... 11 more 18.04-61 9268.954: com.yourkit.h.c.k: SSH: delPortForwarding error: Task java.util.concurrent.FutureTask@3ea383e2 rejected from java.util.concurrent.ThreadPoolExecutor@60cb8835[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 55] 18.04-61 9271.891: com.yourkit.h.a.e: cmd: /home/dpagmk/software/jdk1.7.0_45/jre/bin/java -cp .yjp/2018.04-b61/lib/yourkit.jar -Dfile.encoding=UTF-8 -Dyk.logger.stdErrOnly=true com.yourkit.Main -host-server request: getVmInfos exit code: -1 stdout: # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00007f509d520ac0, pid=16167, tid=139984557885184 # # JRE version: Java(TM) SE Runtime Environment (7.0_45-b18) (build 1.7.0_45-b18) # Java VM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode linux-amd64 compressed oops) # Problematic frame: # C [libyjpagent.so+0x14cac0] Options::containsSubstring(YStringImpl<char> const&)+0x0 # # Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again # # An error report file with more information is saved as: # /home/admin/hs_err_pid16167.log # # If you would like to submit a bug report, please visit: # http://bugreport.sun.com/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. # 18.04-61 9278.271: com.yourkit.b.bh: LC@3f153e3{localhost:61219,pid=23720}: created; local=false 18.04-61 9279.359: com.yourkit.b.q: snapshot_outer: starting: null 18.04-61 9279.359: com.yourkit.b.q: controller telemetry limit: 3600 18.04-61 9279.359: com.yourkit.b.q: snapshot_outer: done in 0 sec: null 18.04-61 9313.164: com.yourkit.k.my: error: com.yourkit.util.bs: There's no application running at localhost with profiler agent configured to listen on port 61219 or profiler agent is incompatible with current version of profiler at com.yourkit.api.ControllerImpl.transformException(a:278) at com.yourkit.api.ControllerImpl.makeRequest(a:293) at com.yourkit.api.ControllerImpl.makeRequest(a:534) at com.yourkit.api.ControllerImpl.startCpuProfiling(a:154) at com.yourkit.k.kt.a(a:92) at com.yourkit.k.jo.b(a:60) at com.yourkit.k.d.al.a(a:183) at com.yourkit.k.dz.run(a:16) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.yourkit.b.bj.read(a:21) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at java.io.DataInputStream.readFully(DataInputStream.java:195) at java.io.DataInputStream.readLong(DataInputStream.java:416) at com.yourkit.runtime.Packet.readHeader(a:3) at com.yourkit.runtime.Packet.readHeader(a:51) at com.yourkit.runtime.Packet.readHeaderWithStandardResponse(a:17) at com.yourkit.api.ControllerImpl.readHeaderWithStandardResponse(a:10) at com.yourkit.api.ControllerImpl.access$100(a:558) at com.yourkit.api.ControllerImpl$3.perform(a:8) at com.yourkit.api.ControllerImpl.makeRequest(a:163) ... 6 more json结构为data,而Go object[key]array中的结构为Data。将map[key]struct.slice改为Data

E.g。

map[key]slice

https://play.golang.org/p/Sh_vKVL-D--