Skip to content

Unmarshal JSON Messages #100

Answered by aler9
swarmt asked this question in Q&A
Feb 27, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

You can convert any ROS message to the JSON format in this way:

type Header struct {
	msg.Package `ros:"std_msgs"`
	Seq         uint32
	Stamp       time.Time
	FrameId     string
}

b, err := json.Marshal(&Header{
	Seq: 33,
	Stamp: time.Now(),
	FrameId: "asd",
})
if err != nil {
	panic(err)
}

resulting in

{"Package":0,"Seq":30,"Stamp":"2022-02-25T03:17:37.548703Z","FrameId":"world"}

The problem of your JSON format is that:

  • it uses keys in snake case instead of camel case
  • it represents time in a non-standard way

Therefore, if you still want to use it, you have take into consideration these changes:

package main

import (
	"encoding/json"
	"fmt"
	"time"
	"unicode"

	"github.com/aler9/goro…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@swarmt
Comment options

Answer selected by swarmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants