我有一个JSON形式的联系人。现在我想将它们解码为String数组。有两个数组; names
和phones
。我使用此代码:
String[] names;
String[] phones;
String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
try {
JSONArray jsonArray = new JSONArray(test);
JSONObject jsonObject = new JSONObject(jsonArray.toString());
Log.i("INFO", String.valueOf(jsonObject.length()));
} catch (JSONException e) {
e.printStackTrace();
}
这会产生错误。如何在names
阵列和phones
阵列中的所有手机中添加所有名称。与names[0]
一样,A
分配了名字,phones[0]
分配了911
,这是与名字对应的第一个电话号码。我怎么能这样做,我是android的新手?
答案 0 :(得分:2)
问题出在这一行
JSONObject jsonObject = new JSONObject(jsonArray.toString());
您正在尝试转换JSONArray
中的JSONObject
,但您无法转换JSONObject
。如果你想访问JSONArray
内的 String[] names;
String[] phones;
String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
try {
JSONArray jsonArray = new JSONArray(test);
phones = names = new String[jsonArray.length()];
for(int i = 0 ; i < jsonArray.length(); i ++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
names[i] = jsonObject.getString("name");
phones[i] = jsonObject.getString("phone");
Log.i("INFO", "name : " + jsonObject.getString("name") + " , phone : " + jsonObject.getString("phone"));
}
} catch (JSONException e) {
e.printStackTrace();
}
,你必须循环遍历每个,或者你可以通过它的索引获得特定的对象。
public IEnumerable<voucher> index(string word, string DivisionCode, string yearlabel, int pageno)
{
using (IDbConnection dbConnection = Connection)
{
var data = dbConnection.Query<fydates>(@"SELECT REPLACE(CONVERT(NVARCHAR(10),fystart,6),' ','-') as fystart,
REPLACE(CONVERT(NVARCHAR(10),fyend,6),' ','-') as fyend FROM fydates WHERE yearlabel = @yearlabel",
new { yearlabel = yearlabel }).Single();
List<voucher> DataQuery = dbConnection.Query<voucher, accounts, division, LockStatus, voucher>(@"SELECT V.id,V.voucherno,
CASE WHEN V.vouchertype='P' THEN 'Paid' ELSE 'Receipt' END AS vouchertype,
REPLACE(CONVERT(NVARCHAR(10),V.dated,6),' ','-') AS dated,V.subno,V.amount,COALESCE(V.syscreated,' ') AS syscreated,V.bankcode,
COALESCE(V.cheqno,' ') AS Cheqno,V.Cheqdate,V.posted,V.currency,A.id as accountsid,A.achead,A.shortname,
D.id as divisionid,D.divname,D.divncode,L.id as lockstatusid,L.name FROM voucher V
LEFT JOIN Accounts A ON V.bankcode = A.mainac
LEFT JOIN divisionmst D ON V.divisionid = D.divncode
LEFT JOIN LockStatus L ON V.posted=L.Type
WHERE (V.voucherno + V.Subno+V.cheqno + A.achead + LEFT(yearvoucherno,3) LIKE @word AND
D.divncode LIKE @DivisionCode) AND (dated BETWEEN @fystart AND @fyend OR dated='31-Mar-49')
Order By V.id DESC OFFSET @pageno *25 ROWS FETCH NEXT 25 ROWS only",
(voucher, accounts, division, LockStatus) =>
{
voucher.accounts = accounts;
voucher.division = division;
voucher.LockStatus = LockStatus;
return voucher;
},
new { word = word, fystart = data.fystart, fyend = data.fyend, DivisionCode = DivisionCode, pageno = pageno - 1 },
splitOn: "accountsid, divisionid, lockstatusid").ToList();
return DataQuery;
}
}
答案 1 :(得分:1)
您无法将json arrya转换为json对象:
try {
JSONArray jsonArray = new JSONArray(test);
for(int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Your code goes here..
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
这是必需的代码。
ArrayList<String> names = new ArrayList<>();
ArrayList<String> phones = new ArrayList<>();
String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
try {
JSONArray jsonArray = new JSONArray(test);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String phone = jsonObject.getString("phone");
names.add(name);
phones.add(phone);
Log.i("INFO", name + ", " + phone);
}
} catch (JSONException e) {
e.printStackTrace();
}
上面你使用了简单的数组,它们只能存储固定大小的数据。但是arrayList和List可以存储可变大小的数据,或者你不需要修改它们的大小。