Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by foxed-art · Dec 13, 2020 at 10:07 PM · jsoncoordinateslocationparsingdeserialize

Paring json Array object

I have a quite specific problem when deserializing an json object.

I have following json data (from a routing api):

 "type": "Feature",
             "name": "ShapeMeta",
             "geometry": {
                 "type": "LineString",
                 "coordinates": [
                     [ 10.77288, 51.84581 ],
                     [ 10.77284, 51.84605 ]
                 ]
             }

and I am parsing is with JsonUtility.FromJson<Feature>(geoJson); into this class:

 [Serializable]
 public class Feature
 {
     public string type;
     public string name;
     public Geometry geometry;
     public Properties properties;
     public string Shape;
 }
 
 [Serializable]
 public class Geometry
 {
     public string type;
     public object[] coordinates;
 }

but I have no idea, how to parse the coordinates, because ist an array of an array of doubles with no tag...and object[] does not work :)

Does anyone have a hint, where I can start to look? or even a quick solution? Thanks a lot!

Comment
Add comment · Show 2
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 ranch000 · Dec 14, 2020 at 09:55 AM 0
Share

Have you tried

 public List<List<double>> coordinates;

avatar image foxed-art · Dec 14, 2020 at 11:29 AM 0
Share

@ranch000 Thanks for your comment. Yes I tried a List> but this also just gave me a null pointer error :/

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CmdrZin · Dec 14, 2020 at 01:07 AM

Maybe make coordinates a Vector2 array.
then use

 coordinates[0] = new Vector2(geometry.coordinates[0][0].n, geometry.coordinates[0][1].n); 

or something like that.

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 · Dec 14, 2020 at 12:11 PM

Unity's JsonUtility does not support jagged arrays. Only arrays inside classes that are inside arrays are supported. So you can not parse this json text with Unity's JsonUtility. You need to use a different parser. Either use Newtonsoft's Json.NET parser or my SimpleJSON parser which does not require that you create any classes. You can directly read the data. With the Unity extension file you get direct support for Vector2.


So when you do

 var root = JSON.Parse(geoJson);

you can simply do

 Vector2 coords = root["geometry"]["coordinates"];

Comment
Add comment · Show 1 · 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 foxed-art · Dec 14, 2020 at 12:23 PM 0
Share

Thanks @Bunny83 for your solution. I already noticed that jagged arrays don't work with Unity's JsonUtility. So today I build a workaround with Json.NET. See below. Yours does look way smoother, I will take a look ;)

avatar image
0

Answer by foxed-art · Dec 14, 2020 at 12:30 PM

I build a workaround, so I can fill my Class directly with a Vector2 Array (maybe not the best, but it works):

 using Newtonsoft.Json;
 [...]
 dynamic deserialized = JsonConvert.DeserializeObject(geoJson);
 RouteFeature routeFeature = new RouteFeature();
 routeFeature.type = deserialized.type;
 routeFeature.features = new List<Feature>();
 foreach (var item in deserialized.features)
 {
     routeFeature.features.Add(new Feature()
     {
     type = item.properties.type,
         name = item.properties.name,
         geometry = new Geometry()
         {
             type = item.geometry.type,
             coordinates = StringToVector2Array(item.geometry.coordinates.ToString())
         },
         properties = new Properties()
         {
             highway = item.properties.highway,
             profile = item.properties.profile,
             distance = item.properties.distance,
             time = item.properties.time,
             name = item.properties.name
         }
     });
 }
     [...]

 Vector2[] StringToVector2Array(string coordinateString)
     {
         coordinateString = coordinateString.Replace(" ", "");
         coordinateString = coordinateString.Replace("[", "");
         coordinateString = coordinateString.Replace("]", "");
         coordinateString = coordinateString.Replace("\n", "");
         //.Trim(new Char[] { ' ', '[', ']', '\n' });
         string[] strArray = coordinateString.Split(',');
 
         Vector2[] vecArray = new Vector2[strArray.Length/2];
         for(int i = 0; i<strArray.Length/2; i ++)
         {
             vecArray[i] = new Vector2(float.Parse(strArray[i * 2]), float.Parse(strArray[i * 2 + 1]));
         }
         return vecArray;
     }

And here is the tarket Class (Classes actually):

 using System.Collections.Generic;
 using UnityEngine;
 
 public class Geometry
 {
     public string type { get; set; }
     public Vector2[] coordinates { get; set; }
 }
 
 public class Properties
 {
     public string highway { get; set; }
     public string profile { get; set; }
     public string distance { get; set; }
     public string time { get; set; }
     public string name { get; set; }
 }
 
 public class Feature
 {
     public string type { get; set; }
     public string name { get; set; }
     public Geometry geometry { get; set; }
     public Properties properties { get; set; }
 }
 
 public class RouteFeature
 {
     public string type { get; set; }
     public List<Feature> features { get; set; }
 }


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

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Read JSON from Facebook? 0 Answers

Parsing Facebook graph JSON 1 Answer

Origin Placement Android Uity (Coordinates of Tuch positions) 0 Answers

Can't read float array into C# from JSON using Simple JSON 2 Answers

JSON invalid value 1 Answer


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