How to get the strings and convert them into JSON Object ?

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.

Best Answer

Answers

  • VladislavPVladislavP USMember ✭✭

    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.

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    Thanks for the hint @VladislavP but how do i display these content in a activity

  • VladislavPVladislavP USMember ✭✭
    edited June 2017

    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} }";

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    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

  • VladislavPVladislavP USMember ✭✭

    You wrote right variant. Like in example, that I showed you. TextView.Text = json.
    Or what more problems?

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    Button convertButton = FindViewById(Resource.Id.ConvertButton);

            Credentials details = new Credentials();
    
            details.Username = "Sandeep";
            details.Password = "[email protected]";
    
            var json = JsonConvert.SerializeObject(details);
    
            convertButton.Click += (object sender, EventArgs e) =>
            {
    
                Toast.MakeText(ApplicationContext, json, ToastLength.Long).Show();
    
            };
    

    details --> has the collection i need

    but when i convert in json i'm getting --> "{}" <-- in the var json

    var json = JsonConvert.SerializeObject(details);

  • VladislavPVladislavP USMember ✭✭
    edited June 2017

    Do you mean that "{}" is all that you get without any other data that is in details?

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    Yes, that is what I'm getting when i try to convert my object as JSON :(

  • felipe1902felipe1902 BRMember ✭✭

    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

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    @felipe1902 No, not working

    for your code it's still coming the same output as [{}] <--

  • felipe1902felipe1902 BRMember ✭✭

    strange, can you show your class credentials??

    are you sure the command JsonConvert it's from newtonsoft?

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    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

  • felipe1902felipe1902 BRMember ✭✭

    post your code, we will try help you..

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    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
    {

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
    
            //Button viewButton = FindViewById<Button>(Resource.Id.ViewButton);
    
    
        }
    
        protected override void OnResume()
        {
            base.OnResume();
    
            Button convertButton = FindViewById<Button>(Resource.Id.ConvertButton);
    
            //List<Credentials> details = new List<Credentials>();
    
            //details.Add(new Credentials { Username = "Sandeep", Password = "[email protected]" });
    
            var cred = new Credentials()
            {
                Username = "Sandeep", Password = "[email protected]"
            };
    
            //details.Username = "Sandeep";
            //details.Password = "[email protected]";
    
            //IList ls = details as IList;
    
            //IEnumerable ieList = details as IEnumerable;
    
            //var ieTL = ieList.ToString();
    
            //var list = "";
    
            //var dlist = details.ToString();
    
            //list += "{" + cred.Username + " / / " + cred.Password + "}";
    
            var jsondetails = JsonConvert.SerializeObject(cred);
    
            //var jsonuser = JsonConvert.SerializeObject(cred.Username);
            //var jsonpass = JsonConvert.SerializeObject(cred.Password);
    
    
    
            //var jsonList = JsonConvert.SerializeObject(details);
    
            //string dt = JsonConvert.SerializeObject(details);
            //byte[] dataBytes = Encoding.UTF8.GetBytes(dt);
    
            //JSchemaGenerator gen = new JSchemaGenerator();
            //Schema schema = gen.Generate(typeof(Credentials));
    
            /*
            var contentsToWriteToFile = JsonConvert.SerializeObject(details, new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
            });
            */
    
            /*
            convertButton.Click += (object sender, EventArgs e) =>
            {
    
                Toast.MakeText(ApplicationContext, jsonList, ToastLength.Long).Show();
    
    
            };
    
    
    
            convertButton.Click += (object sender, EventArgs e) =>
            {
    
                Toast.MakeText(ApplicationContext, jsonuser, ToastLength.Long).Show();
    
    
            };
    
    
    
            convertButton.Click += (object sender, EventArgs e) =>
            {
    
                Toast.MakeText(ApplicationContext, jsonpass, ToastLength.Long).Show();
    
    
            };
    
            */
    
    
            convertButton.Click += (object sender, EventArgs e) =>
            {
    
                Toast.MakeText(ApplicationContext, jsondetails, ToastLength.Long).Show();
    
    
            };
    
        }
    
    }
    

    }

  • felipe1902felipe1902 BRMember ✭✭

    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

  • SandeepSubbiahSandeepSubbiah INMember ✭✭

    Yeah that's how i have tried, if you could see the commented codes.

  • MizanurRahman.0834MizanurRahman.0834 USMember ✭✭

    You have to convert json data to string and then display

    Credentials details = new Credentials();
        details.Username = "Sandeep";
        details.Password = "[email protected]";
    
        var json = JsonConvert.SerializeObject(details);
    String responseString="";
    using (Stream stream = json.GetResponseStream())
    {
       StreamReader reader = new StreamReader(stream, Encoding.UTF8);
       responseString = reader.ReadToEnd();
    }
        convertButton.Click += (object sender, EventArgs e) =>
        {
    
            Toast.MakeText(ApplicationContext, responseString, ToastLength.Long).Show();
    
        };
    
Sign In or Register to comment.