2017-04-25 60 views
-1

JSONArray包含各種JSONObject S的這個樣子:如何從JSONObject中檢索字符串數組?

{"nearby":0,"x":0,"name":["OPS","BHU"],"y":0} 

{"nearby":0,"x":0,"name":["None"],"y":1} 

{"nearby":0,"x":0,"name":["None"],"y":2} 

{"nearby":0,"x":0,"name":["None"],"y":3} 

{"nearby":0,"x":0,"name":["None"],"y":4} 

{"nearby":0,"x":0,"name":["None"],"y":5} 

{"nearby":0,"x":0,"name":["None"],"y":6} 

{"nearby":0,"x":1,"name":["None"],"y":0} 

{"nearby":0,"x":1,"name":["None"],"y":1} 

{"nearby":0,"x":1,"name":["None"],"y":2} 

{"nearby":0,"x":1,"name":["DDF","THG"],"y":3} 

{"nearby":0,"x":1,"name":["None"],"y":4} 

{"nearby":0,"x":1,"name":["None"],"y":5} 

{"nearby":1,"x":1,"name":["ABC","DEF","XYZ"],"y":6} 

我想在JSONObject,並從這樣一個數組來檢索對應於name鍵值:

JSONObject jo= jsonArray.getJSONObject(i); 
String nearby= jo.getString("nearby"); 
String xString= jo.getString("x"); 
int x=Integer.parseInt(xString); 
String yString= jo.getString("y"); 
int y=Integer.parseInt(yString); 
String[][] name=new String[7][7]; 
name[x][y]= ???????? 

使得輸出是像:

name[0][0]=["OPS","BHU"] 
name[0][1]=["None"] 
name[0][2]=["None"] 
name[0][3]=["None"] 
name[0][4]=["None"] 
name[0][5]=["None"] 
name[0][6]=["None"] 
name[1][0]=["None"] 
name[1][1]=["None"] 
name[1][2]=["None"] 
name[1][3]=["DDF","THG"] 
name[1][4]=["None"] 
name[1][5]=["None"] 
name[1][6]=["ABC","DEF","XYZ"] 
+1

請提供[MCVE] - 確保將其減少到最低限度* *,但完整的例子,以及格式。 –

回答

1

我希望這是你在找什麼f或者,

var arr = [ 
 
    {"nearby":0,"x":0,"name":["OPS","BHU"],"y":0}, 
 
    {"nearby":0,"x":0,"name":["None"],"y":1}, 
 
    {"nearby":0,"x":0,"name":["None"],"y":2}, 
 
    {"nearby":0,"x":0,"name":["None"],"y":3}, 
 
    {"nearby":0,"x":0,"name":["None"],"y":4}, 
 
    {"nearby":0,"x":0,"name":["None"],"y":5}, 
 
    {"nearby":0,"x":0,"name":["None"],"y":6}, 
 
    {"nearby":0,"x":1,"name":["None"],"y":0}, 
 
    {"nearby":0,"x":1,"name":["None"],"y":1}, 
 
    {"nearby":0,"x":1,"name":["None"],"y":2}, 
 
    {"nearby":0,"x":1,"name":["DDF","THG"],"y":3}, 
 
    {"nearby":0,"x":1,"name":["None"],"y":4}, 
 
    {"nearby":0,"x":1,"name":["None"],"y":5}, 
 
    {"nearby":1,"x":1,"name":["ABC","DEF","XYZ"],"y":6}, 
 
    ] 
 

 
    var resultArr = [[],[]]; 
 

 
    $.each(arr, function (key, item) { 
 
    resultArr[item.x][item.y]=item.name; 
 
    }); 
 

 
    console.log(resultArr[0][0]) 
 
    console.log(resultArr[0][1]) 
 
    console.log(resultArr[0][5]) 
 
    console.log(resultArr[1][4]) 
 
    console.log(resultArr[1][6])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>