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 Misk-DHI · Sep 11, 2014 at 12:48 PM · c#deserializationjsonfx

Deserializing Json String

Hi,

Im trying to deserialise a get request where i get a json back with the following

 [{"GameName":"Game1","GameLocation":"Test1"},{"GameName":"Game2","GameLocation":"Test2"}]

Im useing the following script

 using System.Collections;
 using System.Collections.Generic;
 
 namespace APP
 {
 [System.Serializable]
 public class GameFinder
 {
 public string Token;
 public string UserName;
 public string CompanyName;
 public string[] GameAreas;
 public List<string> GameName = new List<string>();
 public List<string> GameLocation = new List<string>();
 
 //public string[] GameName;
 //public string[] GameLocation; 
 
 // public string ProductName;
 
 }
 
 //And im using this

 IEnumerator GetGameList(WWW www)
 {
 yield return www;
 
 if (www.error == null)
 {
 Debug.Log("WWW List Ok!: " + www.text);
 string data = JsonWriter.Serialize(www.text);
 
 apb = JsonReader.Deserialize<APP.GameFinder>(www.text);
 
 }
 else
 {
 Debug.Log("WWW List Error: " + www.error);
 } 
 }


Im getting this error

 InvalidCastException: Cannot cast from source type to destination type.
 Pathfinding.Serialization.JsonFx.JsonReader.Deserialize[GameFinder] (System.String value)






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 niall1111 · Sep 11, 2014 at 02:44 PM

In the same file, declare classes that will hold your JSON data, with variable names that exactly match the variable names in the JSON:

 public class MyJSON{
     public MyJSON(){}
     public GameData[] games
 }
 
 public class GameData{
     public string GameName;
     public string GameLocation;
 }

 public MyJSON jsonData;


Then in your code, you need something like this:

 jsonData = JsonReader.Deserialize<MyJSON>(www.text);

It may take some fiddling around, i'm doing this from memory, but it's the same concept I've used for dealing with JSON anyway...

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 Misk-DHI · Sep 25, 2014 at 12:58 PM 0
Share

I tried your solution, but i still get

 InvalidCastException: Cannot cast from source type to destination type.Pathfinding.Serialization.JsonFx.JsonReader.Deserialize[$$anonymous$$yJSON] (System.String value)Connection$$anonymous$$anager+c__Iterator2.$$anonymous$$oveNext () (at Assets/Scripts/Connection$$anonymous$$anager.cs:145)

I having no problem retriving Token, UserName and CompanyName.

But its not the same layout the string is outputting. I cant change the output of the otherstring btw.

I have verified the string in [http://jsonformatter.curiousconcept.com/][1], to be sure it was valid.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

JsonFX and Polymorphism? 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 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