Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by kshitijgoel · Jun 15, 2016 at 06:21 AM · c#unity 5jsonparsingrobot

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 + "}";
             }
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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"].

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Looking for Code that will Read JSON files (C# code) 2 Answers

Unity json get data 2 Answers

How can i get all of my users data on FireBase and store it as an array using Unity and RestClient json 1 Answer

Null Reference exception when loading json data to dictionary 1 Answer

Issues with JSONUtility 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges