gpt4 book ai didi

android - 同一对象数组中的数组中的json数组?

转载 作者:行者123 更新时间:2023-11-30 03:56:10 25 4
gpt4 key购买 nike

我创建了一个带有 json 解析的小应用程序,因为我正在访问 json 数组但是在这个 jsonarray 中我想要电话号码标题和地址线但是如何访问电话号码我不明白所以如何访问它

这里是json格式

> {
"responseData": {
"results": [
{
"title": "Pakvan Dining Hall",
"phoneNumbers": [
{
"type": "",
"number": "079 26636473"
},
{
"type": "Mobile",
"number": "09427608429"
}
],
"addressLines": [
"Vadilal Sarabhai Hospital",
"Ahmedabad, Gujarat 380006, India"
]
},
{
"title": " Dining Hall",
"phoneNumbers": [
{
"type": "",
"number": "079 26645473"
},
{
"type": "Mobile",
"number": "09427608429"
},
{
"type": "",
"number": "079 26645473"
},
{
"type": "Mobile",
"number": "09427608429"
}
],
"addressLines": [
" Sarabhai Hospital",
"Ahmedabad, Gujarat 380006, India"
]
},
}

ArrayList> contactList = new ArrayList>();

    JsonParser jparser= new JsonParser();
String jsonstringfunction =jparser.jsonstringfunction(url);

Log.e("Jsonstringfunction","n"+jsonstringfunction);

try
{
JSONObject jsonObject2 = new JSONObject(jsonstringfunction);

JSONObject response = jsonObject2.getJSONObject(TAG_RESPONSE);
JSONArray jresults=response.getJSONArray(TAG_RESULTS);
Log.e("check","3333"+jresults);

for (int i = 0; i < jresults.length(); i++)
{
Log.e("check","4444"+jresults.length());
String titlenoformating = jresults.getJSONObject(i).get(TAG_TITLENOFORMATING).toString();

Log.e("Title",""+titlenoformating);

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITLENOFORMATING,titlenoformating);

JSONArray jaddressline=jresults.getJSONObject(i).getJSONArray(TAG_ADDRESSLINES);

Log.e("addressline","addressline"+jaddressline);
String addressline2="";
for(int j=0;j<jaddressline.length();j++)
{
addressline2=addressline2+jaddressline.getString(j).toString();

Log.e("addressline","jaddressline"+addressline2);
}
// adding each child node to HashMap key => value
map.put(TAG_ADDRESSLINES,addressline2);

JSONArray jphonenumbers=jresults.getJSONObject(i).getJSONArray(TAG_PHONENUMBERS);

for(int k=0;k<jphonenumbers.length();k++)
{

Log.e("Phonenumbers","phonenumbers"+jphonenumbers);

JSONArray jtype=jphonenumbers.getJSONObject(k).getJSONArray(TAG_TYPE);

Log.e("JsonPhone","type"+jtype);
for(int m=0;m<jtype.length();m++)
{

String type=jtype.getString(m).toString();

Log.e("type","type"+type);
}

}


contactList.add(map);
}

最佳答案

String[] title,number,address;
JSONObject jobject = new JSONObject(your_result_object);
JSONObject json=jobject;
JSONObject jresponse = json.getJSONObject("responseData");
JSONArray jresult = jresponse.getJSONArray("results");

title = new String[jresult.length()];

for(int i=0;i<jresult.length();i++){
JSONObject obj = jresult.getJSONObject(i);
title[i] = obj.getString("title");

JSONArray jphonenumbers = obj.getJSONArray("phoneNumbers");
number = new String[jphonenumbers.length()];

for(int j=0;j<jphonenumbers.length();j++){
JSONObject obj2 = jphonenumbers.getJSONObject(j);
number[j] = obj2.getString("number");
}

JSONArray jaddress = obj.getJSONArray("addressLines");
address = new String[jaddress.length()];
for(int k=0;k<jaddress.length();k++){
address[k] = jaddress.getString(k);
}
}

关于android - 同一对象数组中的数组中的json数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13321457/

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