Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JSONArray:如何转换为ArrayList<;字符串[]>;_Java_Android_String_Arraylist_Casting - Fatal编程技术网

Java 使用JSONArray:如何转换为ArrayList<;字符串[]>;

Java 使用JSONArray:如何转换为ArrayList<;字符串[]>;,java,android,string,arraylist,casting,Java,Android,String,Arraylist,Casting,我有如下的json { "tiles":[ { "header":"Manual", "subheader":"Login", "summary1_label":"", "summary2_label":"", "summary3_label":"", "summary4_label":"", "summary1_value":"",

我有如下的json

{  
   "tiles":[  
      {  
         "header":"Manual",
         "subheader":"Login",
         "summary1_label":"",
         "summary2_label":"",
         "summary3_label":"",
         "summary4_label":"",
         "summary1_value":"",
         "summary2_value":"",
         "summary3_value":"",
         "summary4_value":"",
         "iconUrl":"http://x.x.x.x\/icon1.png"
      },
      {  
         "header":"NewUser",
         "subheader":"Login",
         "summary1_label":"",
         "summary2_label":"",
         "summary3_label":"",
         "summary4_label":"",
         "summary1_value":"",
         "summary2_value":"",
         "summary3_value":"",
         "summary4_value":"",
         "iconUrl":"http://x.x.x.x.x\/icon2.png"
      },
      {  
         "header":"Facebook",
         "subheader":"Login",
         "summary1_label":"",
         "summary2_label":"",
         "summary3_label":"",
         "summary4_label":"",
         "summary1_value":"",
         "summary2_value":"",
         "summary3_value":"",
         "summary4_value":"",
         "iconUrl":"http://x.x.x.x/icon3.png"
      }
   ]
}
public ArrayList<String[]> formatHttpResponse_SummaryTile(JSONObject json) throws JSONException {
    ArrayList<String[]> arrayList_summary = new ArrayList<String[]>();
    JSONArray tilesArray = json.getJSONArray("tiles");
    //JSONObject allTilesData = json.getJSONObject("tiles");
    for (int i=0; i<tilesArray.length(); i++)
    {
        //JSONObject thisJsonObject = tilesArray.getJSONObject(i);
        JSONArray thisJsonArray = tilesArray.getJSONArray(i);
        String[] thisStringArray = new String[thisJsonArray.length()];
        for (int j=0; j<thisJsonArray.length(); j++)
        {
            thisStringArray[j]=thisJsonArray.getString(j);
        }
        arrayList_summary.add(thisStringArray);
    }
    return arrayList_summary;
}
我将其转换为JsonObject,并将其发送到重新格式化的方法。在这种方法中,我执行以下操作

{  
   "tiles":[  
      {  
         "header":"Manual",
         "subheader":"Login",
         "summary1_label":"",
         "summary2_label":"",
         "summary3_label":"",
         "summary4_label":"",
         "summary1_value":"",
         "summary2_value":"",
         "summary3_value":"",
         "summary4_value":"",
         "iconUrl":"http://x.x.x.x\/icon1.png"
      },
      {  
         "header":"NewUser",
         "subheader":"Login",
         "summary1_label":"",
         "summary2_label":"",
         "summary3_label":"",
         "summary4_label":"",
         "summary1_value":"",
         "summary2_value":"",
         "summary3_value":"",
         "summary4_value":"",
         "iconUrl":"http://x.x.x.x.x\/icon2.png"
      },
      {  
         "header":"Facebook",
         "subheader":"Login",
         "summary1_label":"",
         "summary2_label":"",
         "summary3_label":"",
         "summary4_label":"",
         "summary1_value":"",
         "summary2_value":"",
         "summary3_value":"",
         "summary4_value":"",
         "iconUrl":"http://x.x.x.x/icon3.png"
      }
   ]
}
public ArrayList<String[]> formatHttpResponse_SummaryTile(JSONObject json) throws JSONException {
    ArrayList<String[]> arrayList_summary = new ArrayList<String[]>();
    JSONArray tilesArray = json.getJSONArray("tiles");
    //JSONObject allTilesData = json.getJSONObject("tiles");
    for (int i=0; i<tilesArray.length(); i++)
    {
        //JSONObject thisJsonObject = tilesArray.getJSONObject(i);
        JSONArray thisJsonArray = tilesArray.getJSONArray(i);
        String[] thisStringArray = new String[thisJsonArray.length()];
        for (int j=0; j<thisJsonArray.length(); j++)
        {
            thisStringArray[j]=thisJsonArray.getString(j);
        }
        arrayList_summary.add(thisStringArray);
    }
    return arrayList_summary;
}
public ArrayList formatHttpResponse_SummaryTile(JSONObject json)抛出JSONException{
ArrayList ArrayList_summary=新建ArrayList();
JSONArray tilesArray=json.getJSONArray(“tiles”);
//JSONObject allTilesData=json.getJSONObject(“tiles”);
对于(int i=0;i
public ArrayList formatHttpResponse_SummaryTile(JSONObject json))抛出JSONException{
JSONObject thisJsonObject=tileArray.getJSONObject(i);//创建JSONObject从中获取值
迭代器iter=thisJsonObject.keys();
String[]thisStringArray=新字符串[thisJsonObject.length()];
int count=0;//跟踪dest字符串[]中的哪个元素
while(iter.hasNext())
{
String key=iter.next();
尝试
{
字符串值=(字符串)thisJsonObject.get(键);
thisStringArray[count]=值;
}
捕获(JSONException e){
//出了点问题!
打破
}
计数++;
}
arrayList_summary.add(thisStringArray);
}
返回arrayList_摘要;
}

你说的“bugs out”是什么意思你应该让你的json数据更具可读性。很抱歉,编辑人员删除了一些空格。另一件事是,json对象是键-值对,你想把它的键或值作为字符串吗?让问题更清楚。同意osayilgan-你的问题没有多大意义。你有一个json对象tains 1个名称/值对(名称为“tiles”,值为JSON数组)。数组的每个元素都是一个JSON对象,每个元素都有11个名称/值对。您无法直接解析原始JSON字符串int
ArrayList
,并且仍然将其作为原始数据的logicall表示形式。很抱歉,该“for”循环被截断。它与您的循环相同。我也不确定。向上投票或接受作为答案将我非常感激:)当然,我不在comp,但会在fw-hrs中进行测试。:)哦,等等,我应该更明确地说明一个方面-我希望能够循环遍历对象节点,而不必专门调用它们,例如jsonObject[i]而不是jsonObject.getString(“header”)-这可能吗?