2

I'm new to xamarin and to json but I got to the point where I get a response from my php server with an internet browsers with the json code below , I know it is an object array and I made a class for the object using the option in visual studio option (Edit->Past Special->Past JSON as class) I only find how to do a login system but what I want to do is to see the data on a listview .Sorry for my bad English .

{
  "error": false,
  "users": [
    {
      "idCustomer": 38,
      "firstname": "Dxxxx",
      "surname": "Cxxxxx",
      "gamertag": "Mxxxx",
      "email": "dpxxxxx@xxxxxl.com",
      "mobile": "012xxxxxx"
    },
    {
      "idCustomer": 36,
      "firstname": "Pxxxxx",
      "surname": "Axxxxxx",
      "gamertag": "Mxxxxxxx",
      "email": "pxxxxxx@xxxxxxx.com",
      "mobile": "0728468780"
    },
    {
      "idCustomer": 37,
      "firstname": "Dxxxxxx",
      "surname": "Dxxxxxx",
      "gamertag": "Axxxxxxxx",
      "email": "txxxxxxx@xxxxxl.com",
      "mobile": "011xxxxxxx"
    },
    {
      "idCustomer": 39,
      "firstname": "Exxxxxx",
      "surname": "Axxxxxxx",
      "gamertag": "Cxxxxxxx",
      "email": "exxxxxx@xxxxxxxx.com",
      "mobile": "012xxxxxxx"
    },
    {
      "idCustomer": 40,
      "firstname": "Cxxxxxx",
      "surname": "Axxxxxxx",
      "gamertag": "Bxxxxxxx",
      "email": "cxxxxxxx.xxxxxxx@xxxxxxx.com",
      "mobile": "012"
    },
    {
      "idCustomer": 41,
      "firstname": "Lenard",
      "surname": "Neemoid",
      "gamertag": "Gamers",
      "email": "email@email.com",
      "mobile": "10214785"
    },
    {
      "idCustomer": 42,
      "firstname": "Lenard",
      "surname": "Nemoi",
      "gamertag": "Spack",
      "email": "email@email.com",
      "mobile": "123456789"
    },
    {
      "idCustomer": 43,
      "firstname": "test",
      "surname": "test",
      "gamertag": "test",
      "email": "test@test.com",
      "mobile": "123456789"
    },
    {
      "idCustomer": 44,
      "firstname": "test",
      "surname": "tees",
      "gamertag": "tes",
      "email": "ttess",
      "mobile": "11222456"
    },
    {
      "idCustomer": 51,
      "firstname": "Dxxxxxxx",
      "surname": "Lxxxxxxx",
      "gamertag": "Sxxxxxxx",
      "email": "Dxxxxxxx.Lxxxxxxx@Sxxxxxxx.com",
      "mobile": "1234567890"
    }
  ]
}

I have used this code but I do get an error

namespace LANApp
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    private ListView list_user;
    private Button bnt_get_data;

    private ArrayAdapter<string> adapter;
    private SynchronizationContext sc;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);

        list_user = FindViewById<ListView>(Resource.Id.list_user);
        bnt_get_data = FindViewById<Button>(Resource.Id.bnt_get_data);

        adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line);
        adapter.Add("No Data Yet");
        list_user.Adapter = adapter;

        sc = SynchronizationContext.Current;
        bnt_get_data.Click += Bnt_get_data_Click;
    }

    private void Bnt_get_data_Click(object sender, EventArgs e)
    {
        getJSON();
    }


    private async void getJSON()
    {
        adapter.Clear();

        IRestClient client = new RestClient("http://192.xxx.xxx.xxx/Laxxxxxx/public");
        IRestRequest request = new RestRequest("allusers/", Method.GET);

        try
        {
            await Task.Run(() => 
            {
                IRestResponse<List<User>> response = client.Execute<List<User>>(request);
                foreach(var user in response.Data)
                {
                    sc.Post(new SendOrPostCallback(o =>
                    {
                        adapter.Add(o as string);
                        adapter.NotifyDataSetChanged();
                    }), user.gamertag);
                }
            });
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return;
    }
}
}

and the error is Unhandled Exception:

Java.Lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference occurred

and my User class looks like this

namespace LANApp.Model
{
public class User
{
    public int idCustomer { get; set; }
    public string firstname { get; set; }
    public string surname { get; set; }
    public string gamertag { get; set; }
    public string email { get; set; }
    public string mobile { get; set; }
}

public class RootObject
{
    public bool error { get; set; }
    public List<User> users { get; set; }
}
}
AHandyCap
  • 21
  • 3
  • 1
    You need to provide the code relevant to your problem. I think you need to take some time and read about serializing and deserializing Json so you understand it better. At the moment this question is asking people to do your work for you – Kevin Jun 18 '19 at 08:21
  • Sorry about that I'll edit my post – AHandyCap Jun 18 '19 at 08:32

2 Answers2

1

First define a complex object that you can de-serialise your JSON into, it should look something like this:

public class User
{
    public int idCustomer { get; set; }

    public string firstname { get; set; }

    public string surname { get; set; }

    public string gamertag { get; set; }

    public string email { get; set; }

    public string mobile { get; set; }
}

Creating objects from JSON is pretty easy nowadays thanks to Visual Studio, you can create a new class and copy your JSON string then use:

Edit -> Paste Special -> Paste Json As Classes

This will create a complex object from your JSON string like the one above, simple.

Once you've done that then you can pass in your JSON to a de-serialiser like this:

using newtonsoft.json

JsonConvert.DeserializeObject<List<User>>(jsonString);

Further reading:

JoeTomks
  • 3,243
  • 1
  • 18
  • 42
0

Yes, you can use the Newtonsoft.Json Nuget to achieve this, according to your error, you can try to init the public List<User> users as follows

class RootObject

public class RootObject
{
    public bool error { get; set; }

    public List<User> users = new List<User>();  // init the users like this 
}

If the problem persist, you can try to check if the array in your ArrayAdapter contain entry that is null. There must be no nulls there. You can check from the following code first:

IRestResponse<List<User>> response = client.Execute<List<User>>(request);
            foreach(var user in response.Data)
            {
                sc.Post(new SendOrPostCallback(o =>
                {
                    adapter.Add(o as string);
                    adapter.NotifyDataSetChanged();
                }), user.gamertag);
            }

Note: first check the values of the response

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19