gpt4 book ai didi

android - org.json.jsonexception 索引 3 超出范围 0..3)

转载 作者:行者123 更新时间:2023-11-29 15:57:06 24 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,我正在使用 json 解析,我正在尝试从服务器获取响应,我的响应如下,我不知道如何使用单个键访问多个值,有人可以帮助我吗?

{
"filters_id":53,"user_login_id":2650,
"allow":"N",
"mothertongue":"Bhojpuri",
"maritalstatus":
[
"Widow\/Widower","Divorcee","Separated"
],
"min_age":19,
"max_age":22,
"country":"India"
}

protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(FILTER_URL, ServiceHandler.GET);

Log.d("Response: ", "> " + jsonStr);


try {
jsonObj = new JSONObject(jsonStr);


// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(FILTER_ID, jsonObj.getString(FILTER_ID));
map.put(FILTER_USER_LOGIN_ID,jsonObj.getString(FILTER_USER_LOGIN_ID));
map.put(FILTER_ALLOW, jsonObj.getString(FILTER_ALLOW));
map.put(FILTER_MOTHERTONGUE, jsonObj.getString(FILTER_MOTHERTONGUE));
map.put(FILTER_MARITAL, jsonObj.getString(FILTER_MARITAL));
map.put(FILTER_MINAGE, jsonObj.getString(FILTER_MINAGE));
map.put(FILTER_MAXAGE, jsonObj.getString(FILTER_MAXAGE));
map.put(FILTER_COUNTRY, jsonObj.getString(FILTER_COUNTRY));

/*JSONArray status = jsonObj.getString("maritalstatus");
status.getString(0);*/
final String radiovalue = jsonObj.getString("allow");
Log.d("Value: ", "> " + radiovalue);
final String mothertong = jsonObj.getString("mothertongue");
Log.d("Value: ", "> " + mothertong);


/* final String marital = jsonObj.getJSONArray("maritalstatus");
Log.d("Value: ", "> " + marital);*/


/*JSONArray maritalarray = (JSONArray) jsonObj.get("maritalstatus");
final String marital=maritalarray.getString(0);
Log.d("Value: ", "> " + marital);*/


JSONArray msg = (JSONArray) jsonObj.get(FILTER_MARITAL);
for(int i=0;i< FILTER_MARITAL.length();i++)
{
marital= msg.getString(i);

}
Log.d("Value: ", "> " + marital);
/*Iterator<String> iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}*/
final String minimumage = jsonObj.getString("min_age");
Log.d("Value: ", "> " + minimumage);
final String maximumage = jsonObj.getString("max_age");
Log.d("Value: ", "> " + maximumage);
final String countrys = jsonObj.getString("country");
Log.d("Value: ", "> " + countrys);

getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
Intent intent=new Intent(getActivity(),Filter.class);
intent.putExtra("id", id);
intent.putExtra("radiostatus", radiovalue);
intent.putExtra("motherstong", mothertong);
intent.putExtra("marriagestatus", marital);
intent.putExtra("minages", minimumage);
intent.putExtra("maxages", maximumage);
intent.putExtra("countries", countrys);
startActivity(intent);
}
});





} catch (JSONException e) {
e.printStackTrace();
}


return data;
}


protected void onPostExecute(ArrayList<HashMap<String,String>> result) {

super.onPostExecute(result);


// dismiss the dialog after getting all albums
if (pDialog.isShowing())
pDialog.dismiss();
// updating UI from Background Thread
/*Intent mIntent = new Intent(getActivity(),Filter.class);
Bundle extras = mIntent.getExtras();
try {
extras.putString("radiostatus", jsonObj.getString(FILTER_ALLOW));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */


}

最佳答案

marital = "";

JSONArray msg = (JSONArray) jsonObj.get(FILTER_MARITAL);
for(int i=0;i< msg.length();i++)
{
marital+= msg.getString(i);

}

关于android - org.json.jsonexception 索引 3 超出范围 0..3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27437248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com