Is there an easy way to get JSON from a webservice and put it into my SQLite DB in C# mono for android (xamarin)? There are some tedious ways to do it but I want something quick and elegant.
问题:
回答1:
The question is old, but here is the equivalent code of @gghuffer in Java:
public static ContentValues objectToContentValues(Object o) throws IllegalAccessException {
ContentValues cv = new ContentValues();
for (Field field : o.getClass().getFields()) {
Object value = field.get(o);
//check if compatible with contentvalues
if (value instanceof Double || value instanceof Integer || value instanceof R.string || value instanceof Boolean
|| value instanceof Long || value instanceof Float || value instanceof Short) {
cv.put(field.getName(), value.toString());
Log.d("CVLOOP", field.getName() + ":" + value.toString());
} else if (value instanceof Date) {
cv.put(field.getName(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)value));
}
}
return cv;
}
For use this, you need to use the library of Gson
and define java classes for all your json
(i.e. 1 object - 1 json), then, for every class, you need to implement the following piece of code:
public static YourClass fromJSON(String dpJSON) {
Gson gson = new Gson();
return gson.fromJson(dpJSON, YourClass.class);
}
Finally, you have your json string jsonStr
, so:
ContentValues cv = Util.objectToContentValues(YourClass.fromJSON(jsonStr));
Hope this helps
回答2:
I made the following class to handle this when using @SerializedName
annotations, it also adds support for ignoring certain fields. Hope it helps someone.
import android.content.ContentValues;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class ContentValuesWriter {
public static ContentValues objectToContentValues(Object o, Field... ignoredFields) {
try {
ContentValues values = new ContentValues();
//Will ignore any of the fields you pass in here
List<Field> fieldsToIgnore = Arrays.asList(ignoredFields);
for(Field field : o.getClass().getDeclaredFields()) {
field.setAccessible(true);
if(fieldsToIgnore.contains(field))
continue;
Object value = field.get(object);
if(value != null) {
//This part just makes sure the content values can handle the field
if(value instanceof Double || value instanceof Integer || value instanceof String || value instanceof Boolean
|| value instanceof Long || value instanceof Float || value instanceof Short) {
values.put(field.getAnnotation(SerializedName.class).value(), value.toString());
}
else if (value instanceof Date)
values.put(field.getName(), Constants.DATE_FORMAT_FULL.format((Date) value));
else
throw new IllegalArgumentException("value could not be handled by field: " + value.toString());
}
else
Print.log("value is null, so we don't include it");
}
return values;
} catch(Exception e) {
Print.exception(e);
throw new NullPointerException("content values failed to build");
}
}
}
You'll need to replace a few things there which are custom to my app, just the date format and Print function.
回答3:
I made a static class that will convert any object into contentvalues using reflection. I'm sure there's an equivalent way to do this in Java. Put your JSON objects into a class of some sort and this will convert all of the properties into contentvalues.
public static class Util
{
//suck all of the data out of a class and put it into a ContentValues object for use in SQLite Database stuff
public static ContentValues ReflectToContentValues(object o)
{
ContentValues cv = new ContentValues();
foreach (var props in o.GetType().GetProperties())
{
object val = props.GetValue(o, null);
//check if compatible with contentvalues (sbyte and byte[] are also compatible, but will you ever use them in an SQLite database?
if (props.CanRead && props.CanWrite && (val is double || val is int || val is string || val is bool || val is long || val is float || val is short))
{
cv.Put(props.Name, val.ToString());
Log.Info("CVLOOP", props.Name + ":" + val.ToString());
}
else if (val is DateTime)
cv.Put(props.Name, ((DateTime)val).ToString("yyyy-MM-dd HH:mm:ss"));
}
return cv;
}
}