Need help with scripting problems - API

Hey, I’m a noob, currently doing an assignment.

I don’t understand most terms.

I’m trying to create a simple weather API function
There seems to be no issues with the code, but I get errors, and the api doesn’t seem to show the URL when adding it as a component in the EventSystem section.

7120045--850051--upload_2021-5-8_14-47-22.png

The errors are:

Assets\Scripts\WeatherApi.cs(57,38): error CS1061: ‘string’ does not contain a definition for ‘Parse’ and no accessible extension method ‘Parse’ accepting a first argument of type ‘string’ could be found (are you missing a using directive or an assembly reference?)

and

Assets\Scripts\WeatherApi.cs(39,31): error CS1061: ‘string’ does not contain a definition for ‘text’ and no accessible extension method ‘text’ accepting a first argument of type ‘string’ could be found (are you missing a using directive or an assembly reference?)

I’m quite lost.

I’ve tried reinstalling Unity. Something is missing but I’m no veteran. The script, so far, in the API is below:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
using UnityEngine.UI;

public class WeatherAPI : MonoBehaviour
{
    // store API location into a string
    public string url = "https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139";
    public string currentLocation;
    public float currentTemp;
    public float currentLowTemp;
    public float currentMaxTemp;

    // Display Inputs
    public Text displayLocation;
    public Text displayTemp;
    public Text displayLowTemp;
    public Text displayHighTemp;



    // set up URL request to locate url
    // access JSON code
    // initialisation of API
    IEnumerator Start()
    {
        // set up www request
        WWW request = new WWW(url);
        yield return request;

        if (request.error == null || request.error == "")
        {
            // successful call
            // parse the JSON code
            Debug.Log("Response:" + request.text);
            string weather = JsonUtility.ToJson(request.text);
            Debug.Log(weather.text);
            SetWeather(request.text);


        }
        else
        {
            // if the call to API has failed
            Debug.Log("Error: " + request.error);
        }



    }

    // Set Weather Variables Function
    void SetWeather(string jsonString)
    {
        var weatherJson = jsonString.Parse(jsonString);
        // issuing variables the data they need
        currentLocation = weatherJson["name"].Value;
        currentTemp = weatherJson["main"]["temp"].AsFloat;
        currentLowTemp = weatherJson["main"]["temp_min"].AsFloat;
        currentMaxTemp = weatherJson["main"]["temp_max"].AsFloat;
        Debug.Log(currentLocation);
        Debug.Log(currentTemp);
        Debug.Log(currentMaxTemp);
        Debug.Log(currentLowTemp);
    }

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

The important parts of an error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

2 Likes

This line makes no sense at all:
var weatherJson = jsonString.Parse(jsonString);

Maybe you did a plaintext search and replace, somehow? Because the line should read

var weatherJson = JSON.Parse(jsonString);

1 Like

thank you, this clears up one of the errors. For some reason debug and public text doesn’t light up still

the error is
Assets\Scripts\WeatherAPI.cs(39,31): error CS1061: ‘string’ does not contain a definition for ‘text’ and no accessible extension method ‘text’ accepting a first argument of type ‘string’ could be found (are you missing a using directive or an assembly reference?)

Delete the .text from both line:

Debug.Log(weather.text);
SetWeather(request.text);

Also why are you sending the request to the SetWeather?

All on these notes, my advise is that you need to go back to the basics, you don’t know the basic syntax.
Here is a good one:

https://www.youtube.com/watch?v=FPeGkedZykA

Thanks, I don’t know the basics. I’m copying word from word a tutorial from a teacher.

After doing that I get:

Assets\Scripts\WeatherAPI.cs(40,24): error CS1503: Argument 1: cannot convert from ‘UnityEngine.WWW’ to ‘string’

Well, it’s far from word for word. Check your tutorial again. And watch what I linked.

Thanks, I was having a dyslexic moment. All good now.