Xamarin c#for Android:创建json字符串?
此代码有问题Xamarin c#for Android:创建json字符串?,c#,android,json,xamarin,C#,Android,Json,Xamarin,此代码有问题 GlodalVariables.SoftID = "55"; WebClient client = new WebClient (); Uri uri = new Uri ("http://www.example.com/CreateEntry.php"); string folder = System.Environment.GetFolderPath (System.Environment.SpecialFol
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 ();
当我将完整的json字符串复制到json测试站点()
它告诉我刺是无效的
我正在获取所有记录行,并将它们放入一个json字符串中,然后再将它们推送到服务器。您不应该以数组形式获取结果吗?因此,最终的json应该如下所示:
[{json1},{json2}..]
可能如果您只有
{json1}{json2}
这不是一个有效的对象。另一种解决方案是构建一个“MySMSLog”对象的集合,然后通过JSonConvert序列化该集合吗?这样,您就不必担心通过自己的字符串操作来获得正确的结构
这很可能也会证明您的代码将来会发生JSON标准更改的可能性,因为NewtonSoft库也会更新 嗯。。。所以在组合成字符串后,我应该在开头加“[”,在结尾加“]”。。。以及将“”改为“”,谢谢!!已测试并成为有效字符串。。很有魅力!!