我有像json字符串这样的变量我需要读取它来获取每个值
String data ={"sid":"48","name":"perla","email":"perla@gmail.com","pass":"613eb6402eb5164"};
我试图用这些方法读取字符串:
String sid = data("sid");
String sid = data(0);
String sid = data[0];
但效率不高
这是一个字符串而不是数组字符串
答案 0 :(得分:0)
如果你有像
这样的字符串 String data = "{sid:48, name:perla, email:perla@gmail.com, pass:613eb6402eb5164}";
您可以将其解析为:
private void getData(String dataString) throws JSONException {
JSONObject jsonObject = new JSONObject(dataString);
String sid = jsonObject.getString("sid");
String name = jsonObject.getString("name");
String email = jsonObject.getString("email");
String pass = jsonObject.getString("pass");
}
答案 1 :(得分:0)
我使用jsonobject
找到了解决方案String data ="{"sid":"48","name":"perla","email":"perla@gmail.com","pass":"613eb6402eb5164"}";
JSONObject jobj = new JSONObject();
try {
jobj = new JSONObject(data);
Log.d(LOG_TAG, jobj.toString());
String order = jobj.getString("orderId");
} catch (JSONException e) {
//e.printStackTrace();
}