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
0
Question by danweaver12 · Mar 11, 2020 at 11:34 AM · jsonwebrequest

Json data isn't being parsed

I have the following JSON data and this is how it appears in the Visual Studio watch window:

 "{\"TestResponse\":\"[{isValid:false,errorMessage:\'\',mainPath:~/yourName/yourfolder/,fileName:\'tempFile.txt\'}]\"}"


So I made two classes:

 [System.Serializable]
 public class ResponseData
 {
     public bool isValid;
     public string errorMessage;
     public string mainPath;
     public string fileName;
 }
 
 [System.Serializable]
 public class ResponseDataList
 {
     public List<ResponseData> TestResponse;
 }

And here's how I'm parsing it:

 ResponseDataList responseDataList = JsonUtility.FromJson<ResponseDataList >(jsonData);

I'm always getting zero elements in the responseDataList though. I tried changing the bool to a string, and the errorMessage to a character (because it looks like it an empty character '\') but no luck.

Comment
Add comment · Show 8
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 · Mar 11, 2020 at 12:06 PM 1
Share

Hi, try with this JSON string

         string jsonData = "{  \"TestResponse\":  [{      \"isValid\": true,      \"error$$anonymous$$essage\": null,      \"mainPath\": \"path\",     \"fileName\": \"fname\"    }  ]}";
 

avatar image danweaver12 ranch000 · Mar 11, 2020 at 12:33 PM 0
Share

Nope I still get zero elements when using that :(

avatar image ranch000 danweaver12 · Mar 11, 2020 at 12:59 PM 0
Share

Ok I'm not sure if Unity's JSON can parse custom class objects (ResponseDataList in this case). Perhaps you will need to use $$anonymous$$iniJSON or NewtonJSON

EDIT: @TreyH is correct. Remove $$anonymous$$onobehaviour and use the jsonData I mentioned.

avatar image TreyH · Mar 11, 2020 at 01:03 PM 0
Share

I don't think you can deserialize $$anonymous$$onoBehaviour instances...

avatar image danweaver12 TreyH · Mar 11, 2020 at 01:07 PM 1
Share

Woops, I got rid of that. Still same issue though. I'll edit my post.

avatar image TreyH danweaver12 · Mar 11, 2020 at 01:38 PM 0
Share

I'm not getting any weird behavior:

 void Start()
 {
     var data = new ResponseData();
     data.isValid = true;
     data.error$$anonymous$$essage = "Error!";
     data.mainPath = "../$$anonymous$$ain";
     data.fileName = "file.txt";
 
     var list = new ResponseDataList();
     list.TestResponse = new List<ResponseData>();
     list.TestResponse.Add(this.data);
 
     var json = JsonUtility.ToJson(list);
     var listDeserialized = JsonUtility.FromJson<ResponseDataList >(json);
 
     Debug.Log(listDeserialized.TestResponse.Count);
     Debug.Log(listDeserialized.TestResponse[0].mainPath);
 }


prints:

 1
 ../$$anonymous$$ain


as expected. How are you serializing it?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Mar 11, 2020 at 01:55 PM

Well, like ranch000 pointed out your json is not valid json at all. The keys and also string values always have to be double quoted. No other quotes are allowed. Here's a full description of the json format. Your string actually does represent valid json, but not what you think it contains. All you got is an object with a single key-value pair. Without all the backslash escaping your json looks like this:

 {
     "TestResponse": "[{isValid:false,errorMessage:'',mainPath:~/yourName/yourfolder/,fileName:'tempFile.txt'}]"
 }

So from the json's point of view TestResponse is just a string, nothing else. The content of that string is not valid json, even when it wasn't put into double quotes.


Like ranch000 said your actual json text should look like this

 {
     "TestResponse": [
         {
             "isValid":false,
             "errorMessage":"",
             "mainPath":"~/yourName/yourfolder/",
             "fileName":"tempFile.txt"
         }
     ]
 }

With backslash escaping and without the whitespace it should look like this

 // C#
 string jsonText = "{ \"TestResponse\": [{ \"isValid\":false, \"errorMessage\":\"\", \"mainPath\":\"~/yourName/yourfolder/\", \"fileName\":\"tempFile.txt\" }]}";

This should parse correctly with the classes you have there. If you got your "json" from a web service, the issue is most likely in that web service as the returned data is not in the format you expected.

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 danweaver12 · Mar 11, 2020 at 01:59 PM 0
Share

Thank you for the detailed explanation! I thought something looked weird with the format, but I never had a problem with the web service before so thought it might be something I was doing.

avatar image
1

Answer by ShadyProductions · Mar 11, 2020 at 01:43 PM

With JsonUtility it seems to work with this:

 string jsonData = "{\"TestResponse\":[{\"isValid\":true,\"errorMessage\":\"null\",\"mainPath\":\"path\",\"fileName\":\"fname\"}]}";

Here is a formatted overview:

 {
   "TestResponse": [
     {
       "isValid": true,
       "errorMessage": "null",
       "mainPath": "path",
       "fileName": "fname"
     }
   ]
 }
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

127 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

Related Questions

SSLHandshakeException during Web Request 0 Answers

How to constantly get new JSON information with GET request from a stream? Help!! 0 Answers

Posting JSON-RPC with WebRequest 0 Answers

How do I get into my StreamingAssets folder for the Web? 0 Answers

Issue assigning JSON Data to Class Structure 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