- Home /
How do I parse JSON from an API and import the variables into a gameobject component during runtime?
Hello!
I am working in Unity, and while I have a beginner understanding of C# workflow in the Unity. I am working on a project that uses API get requests during runtime. I am trying to parse JSON from an API url using API Buddy, a tool from Unity Asset Store, and I am having trouble importing the parsed JSON as variables into the scene.
I'll list out the basic workflow I'm trying to accomplish, and the code, please let me know what clarification you need, this is my first time posting.
process:
user enters input in a text box
the input is added as a field on the end of a get request to an API
the get request is sent to the API url
parse the JSON response
serialize the JSON and import the parsed integers and strings into a Unity gameobject (I think)
....repeat in a loop with a a delay/sleep time or a refresh/submit button
I'm trying to import a stream of json data and integrate it into the game realtime. HERE IS THE CODE:
using UnityEngine;
using System.Collections;
using PerspecDev;
public class YouTubeReader : MonoBehaviour {
// SteamResponseData reflects the API JSON response, but only contains the fields we actually want to retrieve.
// No need to include every JSON field that the API provides here. Just the ones we want. These field names do
// need to match the JSON response fields returned by the API. The class name can be whatever you want.
public class YouTubeResponseData : APIBuddy.WebResponseData {
public Item[] items; // This matches the "items" array element in the JSON response.
public class Item : APIBuddy.WebResponseData {
// These match the "EXACT" elements for each item in the JSON response.
public Snippet snippet;
public Statistics statistics;
public class Snippet : APIBuddy.WebResponseData {
public string title;
public string channelTitle;
}
public class Statistics : APIBuddy.WebResponseData {
public int viewCount;
public int likeCount;
public int dislikeCount;
}
}
}
// Simply pass in the URL for the API endpoint and wait for the response...
string url = "https://www.googleapis.com/youtube/v3/videos?id=pPw_izFr5PA&key=********KEY******&part=snippet,contentDetails,statistics,status";
APIBuddy.GetInstance().SendGetRequest<YouTubeResponseData>(url, 30.0f, (bool success, string errorMessage, int statusCode, object responseObject) => {
if (success) {
// APIBuddy handles populating SteamResponseData for us, based on the fields we set up in it up above. All we need to do now is
// cast responseObject to the SteamResponseData type and then read our data!
YouTubeResponseData youTubeResponseData = (YouTubeResponseData)responseObject;
foreach (YouTubeResponseData.Item item in youTubeResponseData.items) {
public string title = item.snippet.title;
public string channel = item.snippet.channelTitle;
public int views = item.statistics.viewCount;
public int likes = item.statistics.likeCount;
public string dislikes = item.statistics.dislikeCount;
}
} else {
return public string error = "Oops!Doesnt work\n";
}
});
}
}
I want the get requests to happen during runtime, and to be as light/performant as possible.
Thanks in advance for looking.
Joel
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Saving JSON file in Android,How to save json file in android 0 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
How can I figure if an Object was created at runtime? (for extending Json save/load) 0 Answers