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 mustang4484 · Jun 04, 2021 at 09:35 AM · jsonapihttpwebrequestrequestdeserialize

Problem deserialize Json api rest

Hi guys I've try to get some api rest value from unity. I've some problem because sometimes is impossible to deserialize the value inside json. In this example i wouldlike to read location.name :

FROM JSON

 "location": {
     "name": "London",
     "region": "City of London, Greater London",
     "country": "United Kingdom",
     "lat": 51.52,
     "lon": -0.11,
     "tz_id": "Europe/London",
     "localtime_epoch": 1622795128,
     "localtime": "2021-06-04 9:25"
 },

this is the erroe when i try to get this value "ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". When i debug jasonresponse i can see all value inside json file, but the problem is deserialize.

Any suggestion? THX

         HttpWebRequest (HttpWebRequest)WebRequest.Create(String.Format("http://api.weatherapi.c"));
         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
         StreamReader reader = new StreamReader(response.GetResponseStream());
         string jsonResponse = reader.ReadToEnd();
         WindMph info = JsonUtility.FromJson<WindMph>(jsonResponse);
         Debug.Log(jsonResponse);
          Debug.Log(info.location[0].name.ToString());
 
 [System.Serializable]
 public class WindMph
 {
     public List<Winddd> location;
 }
 [System.Serializable]
 public class Winddd
 {
     public string name;
 }

Moved from answer:


Ok, with this api i can read all weather value but without serialize all jsone, can take also the parameters as ID and name, but it's impossible to access for example to coord, or main or wind....i've seen that with the [ ] it's ok the list but only with { } it's a problem.

 {
     "coord": {
         "lon": -122.08,
         "lat": 37.39
     },
     "weather": [
         {
             "id": 800,
             "main": "Clear",
             "description": "clear sky",
             "icon": "01d"
         }
     ],
     "base": "stations",
     "main": {
         "temp": 282.55,
         "feels_like": 281.86,
         "temp_min": 280.37,
         "temp_max": 284.26,
         "pressure": 1023,
         "humidity": 100
     },
     "visibility": 16093,
     "wind": {
         "speed": 1.5,
         "deg": 350
     },
     "clouds": {
         "all": 1
     },
     "dt": 1560350645,
     "sys": {
         "type": 1,
         "id": 5122,
         "message": 0.0139,
         "country": "US",
         "sunrise": 1560343627,
         "sunset": 1560396563
     },
     "timezone": -25200,
     "id": 420006353,
     "name": "Mountain View",
     "cod": 200
 }                         
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 xxmariofer · Jun 04, 2021 at 09:54 AM

I have never tried serializing one property and i dont know if its posible or how it works, my suggestion would be serializing the fulljson

 [System.Serializable]
 public class Location
 {
     public List location;
 }
 
 [System.Serializable]
 public class Winddd
 {
     public string name;
     public string region;
     public string country;
     public float lat;
     public float lon;
     public string tz_id;
     public float localtime_epoch;
     public string localtime;
 }

 Debug.Log(info.location[0].name);
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

121 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

Related Questions

Paring json Array object 3 Answers

UnityWebRequest not working junt in Android with unity 2018.3.x 2 Answers

Read JSON from Facebook? 0 Answers

JSON problems and IEnumerator 1 Answer

UnityWebRequest returns different errors for right urls 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