android - convert JSON to contentvalues -
is there easy way json webservice , put sqlite db in c# mono android (xamarin)? there tedious ways want quick , elegant.
the question old, here equivalent code of @gghuffer in java:
public static contentvalues objecttocontentvalues(object o) throws illegalaccessexception { contentvalues cv = new contentvalues(); (field field : o.getclass().getfields()) { object value = field.get(o); //check if compatible 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, need use library of gson
, define java classes json
(i.e. 1 object - 1 json), then, every class, need implement following piece of code:
public static yourclass fromjson(string dpjson) { gson gson = new gson(); return gson.fromjson(dpjson, yourclass.class); }
finally, have json string jsonstr
, so:
contentvalues cv = util.objecttocontentvalues(yourclass.fromjson(jsonstr));
hope helps
Comments
Post a Comment