使用GSON序列化自定义类(Serialization of Custom Class Using GSON)
我列出了一个自定义类的对象,我试图使用json序列化,但序列化后的值为0,而不是列表中存储的实际值。
MyCustom类
public class CustomClass extends RealmObject { @Expose() @SerializedName("startID") private int startMessageID; @Expose() @SerializedName("endID") private int endMessageID; @Expose(serialize = false) private boolean syncing = false; }
以下是我用来序列化列表的内容。
GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); Gson gson = builder.create(); Type listType = new TypeToken<List<CustomClass >>() { }.getType(); Log.i("Json", gson.toJson(syncModelList, listType));
上面的代码产生了一个输出
[{"endID":0,"startID":0},{"endID":0,"startID":0}]
结构是正确的但我的值丢失了,我已经检查了序列化之前的值,它们是正确的并且存在。
I list of objects of a custom class which i am trying to serialize using json but the values once serialized are 0 and not the actual values that are stored in the list.
MyCustom Class
public class CustomClass extends RealmObject { @Expose() @SerializedName("startID") private int startMessageID; @Expose() @SerializedName("endID") private int endMessageID; @Expose(serialize = false) private boolean syncing = false; }
The following is what i use to serialize the list.
GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); Gson gson = builder.create(); Type listType = new TypeToken<List<CustomClass >>() { }.getType(); Log.i("Json", gson.toJson(syncModelList, listType));
The above code produces an output of
[{"endID":0,"startID":0},{"endID":0,"startID":0}]
The structure is correct but my values are lost,i have checked the values before serialization and they are correct and exist.
原文:https://stackoverflow.com/questions/44993852
最满意答案
SqlLite保存的日期时间格式有2种,称为通用格式,可在所有应用程序中访问。 这有助于我们从本地化问题中解脱出来。 sqlite日期格式DateTime 2012-02-20 16:42:10.000
DataGrid显示这是因为它提取你的系统格式,即DD / MM / YYYY,如果你需要以相同的格式显示它,你需要声明它是明确的。 例如Date.ToUniversalTime()datagridview日期格式20/02/2012 16:42:10
There are 2 things in this, Datetime format that SqlLite is saving is called Universal Format and its accessible across all application. This helps us to save us from Localisation issues. sqlite date format DateTime 2012-02-20 16:42:10.000
DataGrid is showing this because its picking up your system format i.e. DD/MM/YYYY, if you need to show it in same format you will need to declare is explictily. e.g. Date.ToUniversalTime() datagridview date format 20/02/2012 16:42:10
相关问答
更多-
在用DataGridView填充DataGridView之后,该列已经存在于DataGridView ,因此获取并使用它进行排序: DataGridViewColumn col = dataGridView1.Columns["ItemID"]; dataGridView1.Sort(col,ListSortDirection.Ascending) After filling the DataGridView with data, the column is already present in the ...
-
添加另一个System.Drawing.Image数据类型的列,然后使用System.Drawing.Image.FromFile(...)根据图像列的路径设置图像,如下所示: Dim imageColumn As New DataColumn imageColumn.ColumnName = "ActualImage" imageColumn.DataType = GetType(System.Drawing.Image) dt.Columns.Add(ImageColumn) For Each row ...
-
更新datagridview(update datagridview)[2023-10-21]
在frmViewBookings中添加/编辑现有方法: private void frmViewBookings_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed. UpdateData(); } public void UpdateDat ... -
DataSource for DataGridView不显示表的数据(DataSource for DataGridView is not showing the data of the table)[2022-08-14]
尝试这个: string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; // put your connection string string sql = "SELECT * FROM Authors"; // change your table name SqlConnection connection = new SqlConnection(connectionStri ... -
检查总和的计算: (Double.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString()) + Double.Parse(dataGridView2.Rows[i].Cells[1].Value.ToString())).ToString(); 您使用相同的rowindex计算行的总和,但按itemID更新数据库 当循环你的索引i = 2然后它计数股票(其中itemID = 03)+股票(其中itemID = 02) - >因为项目02的股票没有更新它总 ...
-
就像是 string query = "select * from Kartice where ID=" + combobox.SelectedItem.ID.ToString() + " AND Datum_Izvedbe BETWEEN @SDate AND @EDate"; Something like string query = "select * from Kartice where ID=" + combobox.SelectedItem.ID.ToString() + " AND Datu ...
-
你想要的是名为“SQL查询通知”的MS SQL Server工具,在这里介绍: http : //technet.microsoft.com/en-us/library/ms130764.aspx 。 预先警告,它并不简单,它可能会为多个客户端带来一些扩展问题。 这里有一个很好的教程实现: http : //www.codeproject.com/Articles/144344/Query-Notification-using-SqlDependency-and-SqlCach 关于扩展问题以及如何处理它们 ...
-
遵循以下步骤:1.转到数据集并添加新列2.转到数据绑定源并修改查询。 3.转到datagridview并添加新列 我模拟了与您所说的相同的场景,并且它工作正常。 Under the folder in "C:\Users\YOURUSERNAME\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies" visual studio has a load of compiled things it uses for quick reference. ...
-
SqlLite保存的日期时间格式有2种,称为通用格式,可在所有应用程序中访问。 这有助于我们从本地化问题中解脱出来。 sqlite日期格式DateTime 2012-02-20 16:42:10.000 DataGrid显示这是因为它提取你的系统格式,即DD / MM / YYYY,如果你需要以相同的格式显示它,你需要声明它是明确的。 例如Date.ToUniversalTime()datagridview日期格式20/02/2012 16:42:10 There are 2 things in this, ...
-
在没有HorizontalScroll的DataGridView中显示数据(Displaying data in DataGridView without HorizontalScroll)[2024-04-28]
不(轻松)支持多行。 您可以使用Grid.AutoSizeColumnmode和每列的FillWidth属性调整宽度。 作为另一种想法,为什么不在Grid abd数据绑定附近放置多行文本框到特定列? 将显示更多的文本,但一次显示1行。 Multiple lines isn't (easily) supported. You can adjust the Width with Grid.AutoSizeColumnmode and the FillWidth properties of each column ...