我正在从SQL表中检索单行作为JSONObject。他们看起来像这样但更长:
{"UserId":1111,"FirstName":"null","LastName",:"null","City":"Dallas","State":"Select"}
我想提取值并将它们放在与键对应的Android TextView字段中。空字符串值应替换为空格。我无法用地图做到这一点,这是我最接近我想要的地图。我在TextViews中输入它们,如下所示:
firstname.setText(map.get("FirstName"));
对此问题的任何已建立的方法,无论是列表,数组还是哈希图,都将不胜感激。
答案 0 :(得分:1)
你可以导入org.json;并为每一行使用JSONObject
JSONObject reader = new JSONObject(row);
int userId = reader.getIn("UserId");
String firstName = reader.getString("FirstName");
String lastName = reader.getString("LastName");
String city = reader.getString("City");
String state = reader.getString("State");
答案 1 :(得分:0)
我建议你使用Gson将JSON映射到一个类:
String jsonString = {"UserId":1111,"FirstName":"null","LastName",:"null","City":"Dallas","State":"Select"}
Row row = new Gson().fromJson(jsonString, Row.class);
Row.java在哪里:
public class Row{
@SerializedName("UserId")
String userId;
@SerializedName("FirstName")
String firstName;
@SerializedName("LastName")
String lastName;
@SerializedName("City")
String city;
@SerializedName("State")
String state;
}
答案 2 :(得分:0)
这可能为时已晚,但对某人有帮助,你可以这样做
JSONObject dbJsonObj = new JSONObject(dbData);
HashMap<String, Object> map = new HashMap<>();
Iterator<String> iter = dbJsonObj.keys();
while (iter.hasNext()) {
String key = iter.next();
map.put(key, dbJsonObj.get(key));
}
然后
String firstName = (String) map.get("FirstName");