- Home /
Can't read float array into C# from JSON using Simple JSON
I am trying to integrate ROS and Unity using this repository. Here is the JSON message I received using rosbridge websocket server :
{"topic": "/danaus05/motion_manager/traj_spline",
"msg":
{"header":
{"stamp":
{"secs": 4701,
"nsecs": 820000499},
"frame_id": "", "seq": 131},
"knot_times": [0.0, 0.022315455600619316],
"sample_dt": 0.10000000149011612,
"order": 13,
"coefficients": [10.421317100524902, 0.42876696586608887, 0.0, -4.028246920056988e-13, 9.025688182950908e-12, -7.67222327331283e-26, 3.820084571512169e-24, -4.8910171295495195e-23, -4.0375719823897506e-36, 1.0721875680448314e-34, -9.609371526029533e-34, 0.0, 0.0, 0.0, -18.514339447021484, -0.9034151434898376, 0.0, -1.9118333218631767e-13, 4.2836528595102674e-12, 1.616543134702147e-25, -8.048946608918099e-24, 1.0305409390922075e-22, -1.916259013748819e-36, 5.088674866680114e-35, -4.560672880415734e-34, 0.0, 0.0, 0.0, 0.21829719841480255, 1.1102230246251565e-16, 0.0, 0.0, 0.0, -1.9866208328732932e-41, 9.891499612960662e-40, -1.266450251934222e-38, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.127668857574463, 0.0, -1.3376730279013316e-12, 3.996252981308679e-11, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]},
"op": "publish"}
I'm using the SimpleJSON.cs as the JSONNode class. And here is the class that I created. The execution stops at line 31:
using ROSBridgeLib.std_msgs;
using SimpleJSON;
using UnityEngine;
/**
* Define a Spline message.
* /danaus05/motion_manager/traj_spline
*
*/
namespace ROSBridgeLib
{
namespace teleoperationsandbox
{
public class SplineMsg : ROSBridgeMsg
{
private HeaderMsg _header;
private int _seq, _secs, _nsecs;
private TimeMsg _stampmsg;
private byte _order;
private float _sample_dt;
private float[] _knot_times;
private string _coefficients;
int i;
public SplineMsg(JSONNode msg)
{
//_seq = _header.GetSeq();
//_stampmsg = _header.GetTimeMsg();
//_secs = _stampmsg.GetSecs();
//_nsecs = _stampmsg.GetNsecs();
_order = byte.Parse(msg["order"]);
Debug.Log(_order);
_knot_times = float.Parse(msg["knot_times"]);
//_coefficients = float.Parse(msg["coefficients"]).ToString();
}
public static string getMessageType()
{
return "quadrotor_msgs/Spline";
}
public float GetOrder()
{
return _order;
}
public string GetCoefficients()
{
return _coefficients;
}
public string GetKnotTimes()
{
return _knot_times;
}
public override string ToString()
{
return "quarotor_msgs/Spline [seq=" + ", secs=" + ", nsecs=" +
", order=" + _order + ", knot_times=" + _knot_times + ", coefficients=" + _coefficients + "]";
}
public override string ToYAMLString()
{
return "{\"seq\": " + ", \"secs\": " + ", \"nsecs\": "+", \"order\": " + _order + ", \"knot_times\": " + _knot_times + ", \"coefficients\": " + _coefficients + "}";
}
}
}
}
Answer by elenzil · Jun 15, 2016 at 10:14 PM
assuming JSONNode msg
is in fact the msg
sub-dictionary in the json object, that should work. but are you sure that's the case ? if the overall message is being passed in, then you would need to access msg["msg"]["order"]
.
Answer by Bunny83 · Jun 15, 2016 at 11:05 PM
You can't parse a JSONNode into a byte. JSONNode is a class and in the case of the "order" element it represents a "JSONData" instance. Each JSONNode as casting properties. Next thing is "knot_times" is an array which is represented by a JSONArray instance. You would need to use either a loop that or use Linq.
You should do something like this:
using System.Linq;
public SplineMsg(JSONNode msg)
{
_order = (byte)msg["order"].AsInt;
_knot_times = msg["knot_times"].Children.Select(n=>n.AsFloat).ToArray();
}
Your answer
Follow this Question
Related Questions
Looking for Code that will Read JSON files (C# code) 2 Answers
Unity json get data 2 Answers
Null Reference exception when loading json data to dictionary 1 Answer
Issues with JSONUtility 0 Answers