Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Help with API JSON Please!!

Discussion in 'Scripting' started by lisasims, Jun 13, 2020.

  1. lisasims

    lisasims

    Joined:
    May 28, 2013
    Posts:
    21
    Hi yall,

    so i'm trying to get air quality data in json and it was working yesterday but somehow stopped. And it's driving me crazy! cuz the deadline is soon. FYI I'm not a coder at all so please be nice and easy. And most importantly help!!!

    Here's the code:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Globalization;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7. using UnityEngine.UI;
    8.  
    9. public class AirData : MonoBehaviour
    10. {
    11.     private string url = "http://openapi....";
    12.  
    13.     public AirInfo Info;
    14.     private float timer;
    15.     public float minutesBetweenUpdate;
    16.     public Text pm10ValueText;
    17.     public Text pm25ValueText;
    18.     public Text so2ValueText;
    19.     public Text coValueText;
    20.     public Text o3ValueText;
    21.     public Text no2ValueText;
    22.  
    23.  
    24.     void Update()
    25.     {
    26.         if (timer <= 0)
    27.         {
    28.             StartCoroutine(GetAirData());
    29.             timer = minutesBetweenUpdate * 60;
    30.  
    31.             pm10ValueText.text = "PM10:  " + Info.list.pm10Value;
    32.             pm25ValueText.text = "PM25 :  " + Info.list.pm25Value;
    33.             so2ValueText.text = "SO2 :  " + Info.list.so2Value;
    34.             coValueText.text = "CO :  " + Info.list.coValue;
    35.             o3ValueText.text = " O3 :  " + Info.list.o3Value;
    36.             no2ValueText.text = "NO2  :  " + Info.list.no2Value;
    37.         }
    38.         else
    39.         {
    40.             timer -= Time.deltaTime;
    41.         }
    42.     }
    43.  
    44.     IEnumerator GetAirData()
    45.     {
    46.         UnityWebRequest www = UnityWebRequest.Get(url);
    47.         {
    48.             yield return www.SendWebRequest();
    49.  
    50.             if (www.isNetworkError)
    51.             {
    52.                 yield break;
    53.             }
    54.  
    55.             Info = JsonUtility.FromJson<AirInfo>(www.downloadHandler.text);
    56.  
    57.         }
    58.     }
    59.     [Serializable]
    60.  
    61.     public class AirInfo
    62.     {
    63.         public List list;
    64.       //  public Parm parm;
    65.      //   public ArpltnInforInqireSvcVo ArpltnInforInqireSvcVo;
    66.      //   public int totalCount;
    67.     }
    68.  
    69.     [Serializable]
    70.  
    71.     public class List
    72.     {
    73.         public string coValue;
    74.         public string no2Value;
    75.         public string o3Value;
    76.         public string pm10Value;
    77.         public string pm25Value;
    78.         public string so2Value;
    79.  
    80.     }
    81. }
    And this is the OG JSON but I only need the 6 values.

    Code (CSharp):
    1. public class List
    2.     {
    3.         public string _returnType { get; set; }
    4.         public string coGrade { get; set; }
    5.         public string coValue { get; set; }
    6.         public string dataTerm { get; set; }
    7.         public string dataTime { get; set; }
    8.         public string khaiGrade { get; set; }
    9.         public string khaiValue { get; set; }
    10.         public string mangName { get; set; }
    11.         public string no2Grade { get; set; }
    12.         public string no2Value { get; set; }
    13.         public string numOfRows { get; set; }
    14.         public string o3Grade { get; set; }
    15.         public string o3Value { get; set; }
    16.         public string pageNo { get; set; }
    17.         public string pm10Grade { get; set; }
    18.         public string pm10Grade1h { get; set; }
    19.         public string pm10Value { get; set; }
    20.         public string pm10Value24 { get; set; }
    21.         public string pm25Grade { get; set; }
    22.         public string pm25Grade1h { get; set; }
    23.         public string pm25Value { get; set; }
    24.         public string pm25Value24 { get; set; }
    25.         public string resultCode { get; set; }
    26.         public string resultMsg { get; set; }
    27.         public int rnum { get; set; }
    28.         public string serviceKey { get; set; }
    29.         public string sidoName { get; set; }
    30.         public string so2Grade { get; set; }
    31.         public string so2Value { get; set; }
    32.         public string stationCode { get; set; }
    33.         public string stationName { get; set; }
    34.         public string totalCount { get; set; }
    35.         public string ver { get; set; }
    36.     }
    37.  
    38.     public class Parm
    39.     {
    40.         public string _returnType { get; set; }
    41.         public string coGrade { get; set; }
    42.         public string coValue { get; set; }
    43.         public string dataTerm { get; set; }
    44.         public string dataTime { get; set; }
    45.         public string khaiGrade { get; set; }
    46.         public string khaiValue { get; set; }
    47.         public string mangName { get; set; }
    48.         public string no2Grade { get; set; }
    49.         public string no2Value { get; set; }
    50.         public string numOfRows { get; set; }
    51.         public string o3Grade { get; set; }
    52.         public string o3Value { get; set; }
    53.         public string pageNo { get; set; }
    54.         public string pm10Grade { get; set; }
    55.         public string pm10Grade1h { get; set; }
    56.         public string pm10Value { get; set; }
    57.         public string pm10Value24 { get; set; }
    58.         public string pm25Grade { get; set; }
    59.         public string pm25Grade1h { get; set; }
    60.         public string pm25Value { get; set; }
    61.         public string pm25Value24 { get; set; }
    62.         public string resultCode { get; set; }
    63.         public string resultMsg { get; set; }
    64.         public int rnum { get; set; }
    65.         public string serviceKey { get; set; }
    66.         public string sidoName { get; set; }
    67.         public string so2Grade { get; set; }
    68.         public string so2Value { get; set; }
    69.         public string stationCode { get; set; }
    70.         public string stationName { get; set; }
    71.         public string totalCount { get; set; }
    72.         public string ver { get; set; }
    73.     }
    74.  
    75.     public class ArpltnInforInqireSvcVo
    76.     {
    77.         public string _returnType { get; set; }
    78.         public string coGrade { get; set; }
    79.         public string coValue { get; set; }
    80.         public string dataTerm { get; set; }
    81.         public string dataTime { get; set; }
    82.         public string khaiGrade { get; set; }
    83.         public string khaiValue { get; set; }
    84.         public string mangName { get; set; }
    85.         public string no2Grade { get; set; }
    86.         public string no2Value { get; set; }
    87.         public string numOfRows { get; set; }
    88.         public string o3Grade { get; set; }
    89.         public string o3Value { get; set; }
    90.         public string pageNo { get; set; }
    91.         public string pm10Grade { get; set; }
    92.         public string pm10Grade1h { get; set; }
    93.         public string pm10Value { get; set; }
    94.         public string pm10Value24 { get; set; }
    95.         public string pm25Grade { get; set; }
    96.         public string pm25Grade1h { get; set; }
    97.         public string pm25Value { get; set; }
    98.         public string pm25Value24 { get; set; }
    99.         public string resultCode { get; set; }
    100.         public string resultMsg { get; set; }
    101.         public int rnum { get; set; }
    102.         public string serviceKey { get; set; }
    103.         public string sidoName { get; set; }
    104.         public string so2Grade { get; set; }
    105.         public string so2Value { get; set; }
    106.         public string stationCode { get; set; }
    107.         public string stationName { get; set; }
    108.         public string totalCount { get; set; }
    109.         public string ver { get; set; }
    110.     }
    111.  
    112.     public class Example
    113.     {
    114.         public IList<List> list { get; set; }
    115.         public Parm parm { get; set; }
    116.         public ArpltnInforInqireSvcVo ArpltnInforInqireSvcVo { get; set; }
    117.         public int totalCount { get; set; }
    118.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    It would help if you explained what was going wrong.
     
  3. lisasims

    lisasims

    Joined:
    May 28, 2013
    Posts:
    21
    duh, right.. haha

    the values are not printing in the inspector...
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    On lines 31-36 you're trying to copy values from Info before Info is populated would be my guess.

    Try moving those lines to the end of the coroutine - line 56
     
  5. lisasims

    lisasims

    Joined:
    May 28, 2013
    Posts:
    21
    Thanks but no luck. :( I can print it to the console through Debug. I just can't get it to print in the inspector. Arggghhh. I totally had it working yesterday too. I feel so dumb~