Enter a login "username" & "password", get them, use WebAPI and convert them into JSON Object and display the result of JSON object in XAMARIN Android App.
please help me with this.
It is strange. But I never use it inside Toast. May be you can try with TextView? Or in Console? Just to check, because I do the same actions like you wrote, but in Console and everything is fine.
Answers
To convert string into JSON it is better to use Json.Net (http://www.newtonsoft.com/json). You can get this as Nuget Package. To create json from two strings, you need to create an object with two string fields and after that you can convert it into json-object. Json.Net can do it. You can find code samples here - http://www.newtonsoft.com/json/help/html/SerializeObject.htm.
Thanks for the hint @VladislavP but how do i display these content in a activity
In Activity or in View? Anyway you need a function that will get this data and show it where you need (View or just to use it in code). May be I don't understand you in right way, but I am not sure that it is too difficult to get this content and use inside Views.
Or do you mean that you need to show in TextView exactly JSON-structure?
In this case you can use string.Format() or something like this:
yourTextView.Text = $"{\n\"username\": {userData.userName}, \n\"password\": {userData.password} }";
when clicking a button CONVERT , it should convert the given username and password which present in details object
string json = JsonConvert.SerializeObject(details);
then view content in textview ? <---- Struggling to do
You wrote right variant. Like in example, that I showed you. TextView.Text = json.
Or what more problems?
Button convertButton = FindViewById(Resource.Id.ConvertButton);
details --> has the collection i need
but when i convert in json i'm getting --> "{}" <-- in the var json
var json = JsonConvert.SerializeObject(details);
Do you mean that "{}" is all that you get without any other data that is in details?
Yes, that is what I'm getting when i try to convert my object as JSON
It is strange. But I never use it inside Toast. May be you can try with TextView? Or in Console? Just to check, because I do the same actions like you wrote, but in Console and everything is fine.
I do this:
string dt = JsonConvert.SerializeObject(details);
byte[] dataBytes = Encoding.UTF8.GetBytes(dt);
I don't know if do you want send to api but this works
@felipe1902 No, not working
for your code it's still coming the same output as [{}] <--
strange, can you show your class credentials??
are you sure the command JsonConvert it's from newtonsoft?
I did the same code in another project it is working like charm but i don't know why it is not working in this project anyways thanks guy @VladislavP @felipe1902
post your code, we will try help you..
using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.Collections;
using Newtonsoft.Json;
using static Android.Resource;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Javax.Xml.Validation;
namespace JObj
{
[Activity(Label = "JSon View", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
}
I think you can't show object json attributtes with toast, always will show {}, if do you want to see try with attributtes like
jsondetails.Usename
Yeah that's how i have tried, if you could see the commented codes.
You have to convert json data to string and then display