Migz
Migz

Reputation: 145

Xamarin c# for Android : creating a json string?

Having problems with this code ..

        GlodalVariables.SoftID = "55";

        WebClient client = new WebClient ();
        Uri uri = new Uri ("http://www.example.com/CreateEntry.php");

        string folder = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
        Android.Content.Context myContext = Android.App.Application.Context;

        try{

            string  smsUri = System.IO.Path.Combine (folder, "SL.db");
            string myjson = "";
            SQLiteDatabase Mydb = SQLiteDatabase.OpenDatabase(smsUri , null, DatabaseOpenFlags.OpenReadwrite );
            ICursor SMScursor = Mydb.Query ("MySMSLog", null,null, null,null, null ,"TheDate ASC");

            MySMSLog test = new MySMSLog() ;
            if (SMScursor.MoveToFirst ()) {
                while (!SMScursor.IsAfterLast){

                    string number = SMScursor.GetString(SMScursor.GetColumnIndex("TheNumber"));
                    string name = SMScursor.GetString(SMScursor.GetColumnIndex("TheName"));
                    string date = SMScursor.GetString(SMScursor.GetColumnIndex("TheDate"));
                    string direction = SMScursor.GetString(SMScursor.GetColumnIndex("TheDirection"));
                    string text = SMScursor.GetString(SMScursor.GetColumnIndex("TheText"));
                    string id = SMScursor.GetString(SMScursor.GetColumnIndex("Id"));

                    test.Id = int.Parse(id);
                    test.TheDate = date;
                    test.TheDirection = direction ;
                    test.TheName = name;
                    test.TheNumber = number;
                    test.TheText = text;
                    string output = Newtonsoft.Json.JsonConvert.SerializeObject (test);

                    myjson = myjson + output  + " ";

                    SMScursor.MoveToNext ();
                }
            }

            System.Console.WriteLine (myjson    );
            System.Console.WriteLine();
            SMScursor.Close ();

When i Copy the complete json string into a json test site (http://jsonlint.com/) It tells me the sting is invalid ...

I'm getting all the record rows and putting them into a single json string before pushing them over to the server..

Upvotes: 1

Views: 627

Answers (2)

AJ McK
AJ McK

Reputation: 215

Could an alternative solution be to build a collection of "MySMSLog" objects, then serialise the collection throught JSonConvert? That way you don't need to worry about getting the structure correct through your own string manipulation.

This would most likely also future proof your code on the ridiculously outlandish chance that JSON standards change, as the NewtonSoft library would be updated as well.

Upvotes: 1

mslliviu
mslliviu

Reputation: 1138

Shouldn't you get the result in an array form? So the final json should look like :

[{json1},{json2}..] 

Probably if you have just {json1} {json2} this is not a valid object.

Upvotes: 2

Related Questions