Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Vengent · May 14, 2012 at 03:51 AM · javascriptarrayhashtable

[Solved - Mostly] Hashtables, Arrays, Json and Slicing

So I've got a builtin array to store integers, its declared and initiated fine.

 private var invID = new int[4]; 
 invID = [0,1,2,3];

I've got a www call that pulls down some json information that is then parsed into a hashtable.

 {    "slot1": {
         "name": "Value1",
         "id": 3
      },
      "slot2": {
         "name": "Value2",
         "id": 2
      },
      "slot3": {
         "name": "Value3",
         "id": 1
      },
      "slot4": {
         "name": "Value4",
         "id": 2
      }
 }


 var jsonHash=JSON.ParseJSON(data_get.text);

When I try to set a particular node of the integer array to a value in the hashtable I get the error.

 Type 'Object' does not support slicing.

The code to set the values is as follows;

 invID[0] = int.Parse(jsonHash["slot1"]["id"]);
 invID[1] = int.Parse(jsonHash["slot2"]["id"]);
 invID[2] = int.Parse(jsonHash["slot3"]["id"]);
 invID[3] = int.Parse(jsonHash["slot4"]["id"]);

What am I missing? What exactly is slicing?

I'm not trying to set a multi-dimensional array, simply pull a data value out of it.

Do hashtables support MD? I found some examples on the unity forums that seem to show it.

Update: I don't understand why but I think I found a workaround for this.

You have to cast the sub level hashtables again, for some reason its losing its definition from the called script.

 var tempHash : Hashtable = jsonHash["slot1"];
 invID[0] = int.Parse(tempHash["id"]);

Works successfully, but it kind of defeats the point if it requires all the extra code lines anyways.

I ran into the same problem trying to pass a Hashtable into a function, it seems to revert to object status anywhere outside of local context.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kolban · May 14, 2012 at 04:03 AM

Looking at your code,one immediate thought is that int.Parse() is used to convert a string to an in int ... however, your JSON data looks like the "id" property is already an int. It strikes me that a code fragment such as the following should be tried:

 invID[0] = jsonHash["slot1"].id;
Comment
Add comment · Show 3 · 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 Vengent · May 14, 2012 at 04:07 AM 0
Share

Used the int.parse because I kept getting casting errors from object to int.

Using

invID[0] = jsonHash["slot1"].id;

Results in;

'id' is not a member of 'Object'.

removing the int parse and just using;

invID[0] = jsonHash["slot1"]["id"];

Gets the same slicing error.

avatar image kolban · May 14, 2012 at 04:15 AM 0
Share

Hmmm ... it feels like the "jsonHash" method takes a 1 dimensional parameter which is used as the key to get the value. The value being returned is a JavaScript Object of generic type "Object". However, we want the property called "id" which is not a property of a generic object. We seem to be using a class called JSON which has a static method called "ParseJSON". Where is that class defined?

avatar image Vengent · May 14, 2012 at 04:29 AM 0
Share

It's a script I found on the forums;

http://forum.unity3d.com/threads/38273-Lightweight-UnityScript-X$$anonymous$$L-parser?p=547281&viewfull=1#post547281

It may be the problem, but I think its something in my syntax, just don't understand what.

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

JS objects in Unityscript || BCE0048: Type 'Object' does not support slicing. 0 Answers

Physics.OverlapSphere with a min radius? 1 Answer

Make an array containting multiple classes 1 Answer

expected. Insert a semicolon at the end. When the end that it says, is a } Could someone help us fix this? 2 Answers

Javascript - find a value in an array 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