Reputation: 37
I get a string from UnityWebRequest containing all the information I need.
The problem is that UnityWebRequest.downloadHandler.text
returns a string.
How do I get all variables, like all the names, put into an array of strings?
Below is an example of some code I get.
"{'id': 1, 'name': Bob, 'age': 25},{'id': 2, 'name': Mark, 'age': 32},{'id': 3, 'name': Simon, 'age': 16}";
Upvotes: 1
Views: 171
Reputation: 37
I managed to figure this out. The problem was that my JSON class wasn't written probably. I copied the string UnityWebRequest sent me and saved it to a text file. I then imported this text file into Unity assets folder and from there I used Visual Studios "Paste Special" and that created my JSON class for me.
From there I used JSONUtility.FromJson to read UnityWebRequest and then I could get all the information I needed from the string.
Thanks to Sani Singh Huttunen for providing me clues to solve this problem.
Upvotes: 0
Reputation: 24385
You should parse it as Json
.
But that can be a bit tricky since the data isn't strictly valid Json.
You need to do some reformatting first:
Here is a simple example:
using System;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var jsonString = "{'id': 1, 'name': Bob, 'age': 25},{'id': 2, 'name': Mark, 'age': 32},{'id': 3, 'name': Simon, 'age': 16}";
jsonString = jsonString.Replace("\'", "");
jsonString = Regex.Replace(jsonString, @"\w+", @"""$0""");
jsonString = "[" + jsonString + "]";
var data = JsonConvert.DeserializeObject<Data[]>(jsonString);
}
}
public class Data
{
public int Id {get;set;}
public string Name {get;set;}
public int Age {get;set;}
}
Upvotes: 1