Jesse Jesse - 5 months ago 28
JSON Question

Xamarin Android passing data from class to activity

I made a Web API for my Xamarin Android app. And now I pass through the data from the Web API to android using JSON (JArray). And now I filled up a class with the JSON data, but I'm not sure how to get the data from the class into my android Activity.

Codes:
This is for requesting the JArray in my Web API to the Activity, and convert it to the class I made.

WebClient request = new WebClient();
Uri uri = new Uri("http://IPHERE/Database/api/Surfboards/GetSurfboards");
string surfboardsString = request.DownloadString(uri);
JArray theArray = JArray.Parse(surfboardsString);
Surfboard[] surfboard = (Surfboard[])JsonConvert.DeserializeObject(theArray.ToString());


This is my Surfboard.cs (Class)

class Surfboard
{
public string Name { get; set; }
public double Price { get; set; }
public string Type { get; set; }
public byte[] Picture { get; set; }
public string Description { get; set; }
}


And eventually I have another question, I can upload an image that gets converted to a byte array for the JSON, but how can I turn it back into the image if the byte array is in the android app?

Thanks!

Answer

As all the variables in your class in public we can simply get those values from the object that the values you set by their name.

Surfboard sb= new Surfboard();
// set values of sb///

////
// get values of sb
string name=sb.Name;

Answer to your second question : You can turn byte array into image like following.

Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);