简体   繁体   English

Xamarin c#for Android:创建一个json字符串?

[英]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 ... 当我将完整的json字符串复制到json测试站点( http://jsonlint.com/ )时它告诉我刺痛无效...

I'm getting all the record rows and putting them into a single json string before pushing them over to the server.. 我将获取所有记录行并将它们放入单个json字符串中,然后将它们推送到服务器。

Shouldn't you get the result in an array form? 你不应该以数组形式得到结果吗? So the final json should look like : 所以最终的json应该是这样的:

[{json1},{json2}..] 

Probably if you have just {json1} {json2} this is not a valid object. 可能如果你只有{json1} {json2}这不是一个有效的对象。

Could an alternative solution be to build a collection of "MySMSLog" objects, then serialise the collection throught JSonConvert? 另一种解决方案是构建“MySMSLog”对象的集合,然后通过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. 这很可能也是未来证明你的代码有关JSON标准改变的荒谬奇怪的机会,因为NewtonSoft库也会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM