0

I am not sure how to accomplish this. I am getting some users from an www request like this:

Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Authorization", "Basic "+System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("user*pass")));
WWW www = new WWW("https://somedomain.com:8000/users", null, headers);
yield return www;
Debug.Log(www.text);  

The debug returns this:

[{"user_id":"ho896ty6","user_name":"Mikje Flanders","age":43},{"user_id":"ft357hj","user_name":"Anna Simpson","age":56}]

Now, I have an object like this:

public class userData
{
    string user_id;
    string user_name;
    int age;
}

which i would like to get the data into, but not sure when the json is an array. I tried like this, but with no luck:

userData thisUser = JsonUtility.FromJson<userData>(www.text);

Hope someone can help me with this and thanks in advance :-)

Mansa
  • 2,277
  • 10
  • 37
  • 67

2 Answers2

2

1) Install Newtonsoft.json from Nuget Package Manager and add reference to your program like using Newtonsoft.Json;

2) This is your user model class

public class User
{
    public string user_id { get; set; }
    public string user_name { get; set; }
    public int age { get; set; }
}

3) Then deserialize your json to your model like

class Program
{
    static void Main(string[] args)
    {
        //Sample json i get in variable
        var json = @"[{'user_id':'ho896ty6','user_name':'Mikje Flanders','age':43},{'user_id':'ft357hj','user_name':'Anna Simpson','age':56}]";

        //This line convert your string json to c# object
        List<User> userList = JsonConvert.DeserializeObject<List<User>>(json);

        //Loop through to get each object inside users list
        foreach (User user in userList)
        {
            Console.WriteLine($"user_id: {user.user_id},  user_name: {user.user_name}, age: {user.age}");
        }

        Console.ReadLine();
    }
}

Output:

enter image description here

Try once may it help you.

er-sho
  • 9,581
  • 2
  • 13
  • 26
0

you will have to parse the Json using delimiters, unless your using visual studio in that case, check your nuget for Newtonsoft.Json with this package installed you can:

  Using NewtonSoft.Json;

List udat = JsonConvert.DeserializeObject>(json);

   foreach( userData data in udat){
   //do something here to each item;
   }

using jsoneditoronline

[
  {
    "user_id": "ho896ty6",
    "user_name": "Mikje Flanders",
    "age": 43
  },
  {
    "user_id": "ft357hj",
    "user_name": "Anna Simpson",
    "age": 56
  }
]

i cleaned up your code a little so you can see what your doing. added some getters and setters to your class...

it looks like your getting more than one userData return in your request, so you may need a list or array. if you need help with that let me know!

  public class userData
    {
       public string user_id {get;set;}
       public string user_name{get;set;}
       public int age{get;set;}
    }

public class userList
{
public List<userData> users{get;set;}
}
Technivorous
  • 1,682
  • 2
  • 16
  • 22
  • I got an issue with the "udat" in foreach( userData data in udat){}. It has a red line below and I cant get it to work... Hope you can help. – Mansa Sep 22 '18 at 17:48
  • my bad udat.users – Technivorous Sep 22 '18 at 17:50
  • Well, that solved that, but now i got this issue: JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'userList' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. – Mansa Sep 22 '18 at 18:20
  • i updated the code to parse to a list directly, did notice the answer below mine is on the same track @ershoaib! – Technivorous Sep 22 '18 at 18:47
  • Perfect, it did the job :-) – Mansa Sep 22 '18 at 19:25
  • You can't add the Nuget Package Newtonsoft.Json when using unity. When running the game, unity compiler will throw an error about it. – Mohamad Mousheimish Apr 19 '19 at 12:40
  • @MohamadMousheimish, You are in some misconception here. Its perfectly working and acceptable by question owner. You couldn't downvote answers if its not working in your case. Kindly undonwvote answers. – er-sho Apr 19 '19 at 12:45
  • @MohamadMousheimish, Think upon that, If it's throwing the error then question owner doesn't accept an answer. Trying to upgrade and downgrade nuget package of newtosoft either its conflicted with your version of unity. If you ask about this before donwvote then I'll assit you to fix your problem. – er-sho Apr 19 '19 at 12:47
  • @MohamadMousheimish, Which error compiler throwing? Let me know I'll try to provide you a solution. – er-sho Apr 19 '19 at 12:56