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 /
  • Help Room /
avatar image
0
Question by mohsensd1373 · Apr 06, 2020 at 06:53 PM · jsonwebrequest

What is the problem with my code?

my json code :

 {
   "vuser": {
     "id": "1",
     "email": "mohsensd1373@gmail.com ",
     "tel": "000000",
     "fname": "kingblack",
     "username": "mohsensd1373",
     "password": "000000",
     "nfarm": "mohsensd1373"
   },
   "vfarms": {
     "id": "1",
     "name": "mohsensd1373",
     "gift": "0",
     "Planting": "10",
     "power": "0"
   },
   "vPlanting": [
     {
       "id": "1",
       "nfarm": "mohsensd1373",
       "type": "flower",
       "sprite": "flower",
       "timer": "60",
       "result": "flower",
       "posx": "1",
       "posy": "1"
     },
     {
       "id": "2",
       "nfarm": "mohsensd1373",
       "type": "flower",
       "sprite": "flower",
       "timer": "60",
       "result": "flower",
       "posx": "2",
       "posy": "2"
     }
   ]
 }

this code for get json from url and convert to class object: using System; using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using UnityEngine.Networking; class httprequest : MonoBehaviour { void Start() { }

     public string GET(string url)
     {
         
        RootObject myObject = new RootObject();
        
         StartCoroutine(GetText(url, (result) =>{
             PlayerPrefs.SetString("sdall", result);
             //Debug.Log(result); 
         }));
         Debug.Log(PlayerPrefs.GetString("sdall")); 
         myObject = JsonUtility.FromJson<RootObject>(PlayerPrefs.GetString("sdall"));
         Debug.Log(myObject.vuser.email); 
         return myObject.vuser.email;
 
     }
     [Serializable]
 public class VUser
 {
     public string id { get; set; }
     public string email { get; set; }
     public string tel { get; set; }
     public string fname { get; set; }
     public string username { get; set; }
     public string password { get; set; }
     public string nfarm { get; set; }
 }
 
 public class VFarms
 {
     public string id { get; set; }
     public string name { get; set; }
     public string gift { get; set; }
     public string Planting { get; set; }
     public string power { get; set; }
 }
 
 public class VPlanting
 {
     public string id { get; set; }
     public string nfarm { get; set; }
     public string type { get; set; }
     public string sprite { get; set; }
     public string timer { get; set; }
     public string result { get; set; }
     public string posx { get; set; }
     public string posy { get; set; }
 }
 
 public class RootObject
 {
     public VUser vuser { get; set; }
     public VFarms vfarms { get; set; }
     public List<VPlanting> vPlanting { get; set; }
 }
 IEnumerator GetText(string url, Action<string> result)
 {
     UnityWebRequest www = UnityWebRequest.Get(url);
     yield return www.SendWebRequest();
 
     if (www.isNetworkError || www.isHttpError)
     {
         Debug.Log(www.error);
         if (result != null)
             result(www.error);
     }
     else
     {
         //Debug.Log(www.downloadHandler.data); 
         if (result != null)
             result(www.downloadHandler.text);
     }
 }
   
 }


returned this error:

 ArgumentException: JSON parse error: Invalid escape character in string.
 UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <1386288601af43018501cce2912f52f4>:0)
 UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <1386288601af43018501cce2912f52f4>:0)
 httprequest.GET (System.String url) (at Assets/myscript 1/httprequest.cs:21)
 System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <437ba245d8404784b9fbab9b439ac908>:0)
 Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
 System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <437ba245d8404784b9fbab9b439ac908>:0)
 System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <437ba245d8404784b9fbab9b439ac908>:0)
 FlowCanvas.Nodes.PureReflectedMethodNode.Call () (at Assets/ParadoxNotion/FlowCanvas/Modules/FlowGraphs/Nodes/Functions/Reflected/Internal/Method/PureReflectedMethodNode.cs:54)
 FlowCanvas.Nodes.PureReflectedMethodNode+<>c__DisplayClass12_1.<RegisterPorts>b__2 (FlowCanvas.Flow flow) (at Assets/ParadoxNotion/FlowCanvas/Modules/FlowGraphs/Nodes/Functions/Reflected/Internal/Method/PureReflectedMethodNode.cs:80)
 FlowCanvas.FlowOutput.Continue (FlowCanvas.Flow f) (at Assets/ParadoxNotion/FlowCanvas/Modules/FlowGraphs/Ports.cs:272)
 UnityEngine.Logger:LogException(Exception)
 ParadoxNotion.Services.Logger:ForwardToUnity(LogType, Object, String, Object) (at Assets/ParadoxNotion/CanvasCore/Common/Runtime/Services/Logger.cs:114)
 ParadoxNotion.Services.Logger:Internal_Log(LogType, Object, String, Object) (at Assets/ParadoxNotion/CanvasCore/Common/Runtime/Services/Logger.cs:108)
 ParadoxNotion.Services.Logger:LogException(Exception, String, Object) (at Assets/ParadoxNotion/CanvasCore/Common/Runtime/Services/Logger.cs:66)
 NodeCanvas.Framework.Node:Error(Object) (at Assets/ParadoxNotion/CanvasCore/Framework/Runtime/Graphs/Node.cs:326)
 FlowCanvas.FlowOutput:Continue(Flow) (at Assets/ParadoxNotion/FlowCanvas/Modules/FlowGraphs/Ports.cs:276)
 FlowCanvas.FlowOutput:Call(Flow) (at Assets/ParadoxNotion/FlowCanvas/Modules/FlowGraphs/Ports.cs:250)
 FlowCanvas.Nodes.StartEvent:OnStartCallback() (at Assets/ParadoxNotion/FlowCanvas/Modules/FlowGraphs/Nodes/Events/Graph/StartEvent.cs:33)
 NodeCanvas.Framework.GraphOwner:InvokeStartEvent() (at Assets/ParadoxNotion/CanvasCore/Framework/Runtime/Graphs/GraphOwner.cs:384)
 NodeCanvas.Framework.GraphOwner:Start() (at Assets/ParadoxNotion/CanvasCore/Framework/Runtime/Graphs/GraphOwner.cs:375)
 

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
Best Answer

Answer by mohsensd1373 · Apr 06, 2020 at 08:54 PM

I found the problem. The problem was with my json output. There was a space that was not visible in the output. Thank you all

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 · Apr 06, 2020 at 07:13 PM

I see two major issues with your code. First of all Unity's JsonUtility does not serialize properties, only fields. It has the same limitations as the normal serialization system in Unity. So you have to replace your properties with actual public fields.


Your second issue is that you use your "GetText" coroutine to perform the webrequest but you don't wait for the request to finish and try to read the result immediately when you send out the request.


Coroutines run indepentenly from the caller. So you can not make a synchornous webrequest. You have to handle everything in the callback. In your case the code that follows your StartCoroutine call will be executed before your callback is called. So you will read the playerprefs value before it got 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

199 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 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

UnityWebRequest, C# request.accept equivalent? 1 Answer

Issue with sending Json via UnityWebRequest 0 Answers

Issue with WWW/UnityWebRequest when sending JSON to server 1 Answer

UnityWebRequest read body response 0 Answers

UnityWebRequest with "text/event-stream" header doesn't work in webgl build 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