回答編集履歴
1
コード追加
test
CHANGED
@@ -1,2 +1,55 @@
|
|
1
1
|
2 次元配列は、配列の配列です。
|
2
2
|
Json で言うなら、JSONArray の要素が JSONArray ということになります。
|
3
|
+
|
4
|
+
```java
|
5
|
+
import androidx.appcompat.app.AppCompatActivity;
|
6
|
+
|
7
|
+
import android.os.Bundle;
|
8
|
+
import android.util.Log;
|
9
|
+
|
10
|
+
import org.json.*;
|
11
|
+
|
12
|
+
import java.util.Arrays;
|
13
|
+
|
14
|
+
public class MainActivity extends AppCompatActivity {
|
15
|
+
@Override
|
16
|
+
protected void onCreate(Bundle savedInstanceState) {
|
17
|
+
super.onCreate(savedInstanceState);
|
18
|
+
setContentView(R.layout.activity_main);
|
19
|
+
|
20
|
+
try {
|
21
|
+
String[][] a = new String[][]{{"a", "b", "c"}, {"d", "e", "f"}};
|
22
|
+
String str = toJson(a);
|
23
|
+
Log.d("** MainActivity **", "toJson(" + Arrays.deepToString(a) + ")=\"" + str + "\"");
|
24
|
+
String[][] b = fromJson(str);
|
25
|
+
Log.d("** MainActivity **", "fromJson(\"" + str + "\")=" + Arrays.deepToString(b));
|
26
|
+
} catch(JSONException e) {
|
27
|
+
e.printStackTrace();
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
private String toJson(String[][] a) throws JSONException {
|
32
|
+
JSONArray ja = new JSONArray();
|
33
|
+
for(String[] aa : a) {
|
34
|
+
ja.put(new JSONArray(aa));
|
35
|
+
}
|
36
|
+
return ja.toString();
|
37
|
+
}
|
38
|
+
|
39
|
+
private String[][] fromJson(String str) throws JSONException {
|
40
|
+
JSONArray ja = new JSONArray(str);
|
41
|
+
String[][] b = new String[ja.length()][ja.getJSONArray(0).length()];
|
42
|
+
for(int i = 0; i < ja.length(); i++) {
|
43
|
+
JSONArray jaa = ja.getJSONArray(i);
|
44
|
+
for(int j = 0; j < jaa.length(); j++) {
|
45
|
+
b[i][j] = jaa.getString(j);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
return b;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
```
|
52
|
+
```plain
|
53
|
+
D/** MainActivity **: toJson([[a, b, c], [d, e, f]])="[["a","b","c"],["d","e","f"]]"
|
54
|
+
D/** MainActivity **: fromJson("[["a","b","c"],["d","e","f"]]")=[[a, b, c], [d, e, f]]
|
55
|
+
```
|