2016-01-15 17 views
1

有问题,这个代码..Android的Xamarin c#:创建一个json字符串?

 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试验场(http://jsonlint.com/) 它告诉我的刺痛是无效的......

我越来越所有的记录行并将它们放入单个json字符串,然后再将它们推送到服务器。

回答

2

不应该得到数组形式的结果吗?因此,最终的JSON应该是这样的:

[{json1},{json2}..] 

也许,如果你刚{json1} {json2}这不是一个有效的对象。

+0

hmmmm ...所以在组合成字符串后,我应该把“[”放在开头和“]”末尾? ...以及将“”更改为“,” – Migz

+0

是组成阵列 – mslliviu

+0

谢谢!测试,并成为一个有效的字符串..作品像一个魅力! – Migz

1

可以选择一个解决方案来构建“MySMSLog”对象的集合,然后通过JSonConvert序列化集合?这样你就不用担心通过你自己的字符串操作来获得正确的结构。

由于NewtonSoft库也将被更新,这很有可能也会在未来证明您的代码存在JSON标准改变的荒谬可笑的机会。

相关问题