英文:
how to retrieve data from json in java android
问题
我正尝试编辑一个 Android VPN 应用,它有自己的服务器列表。
我有一个包含服务器数据的 JSON,但不知道如何嵌入它。
这是我的 JSON 响应的样式:
[
[
"0",
"PL",
"Poland",
"warswaw",
"1.1.1.1",
"IKEv2",
"",
""
],
[
"1",
"AU",
"Australia",
"Sydney ",
"1.1.1.1",
"IKEv2",
"",
""
],
...
]
以下是应用中的代码:
JSONObject json_response = new JSONObject("http://example.com/json.php");
JSONArray jsonArray = json_response.getJSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json_object = jsonArray.getJSONObject(i);
ServerArray[i][0] = json_object.getString("id");
ServerArray[i][1] = json_object.getString("file");
ServerArray[i][2] = json_object.getString("city");
ServerArray[i][3] = json_object.getString("country");
ServerArray[i][4] = json_object.getString("image");
ServerArray[i][5] = json_object.getString("ip");
ServerArray[i][6] = json_object.getString("active");
ServerArray[i][7] = json_object.getString("signal");
NumServers = NumServers + 1;
}
数组来自旧 API:https://raw.githubusercontent.com/gayanvoice/android-vpn-client-ics-openvpn/images/appdetails.json
如何修改代码以读取我的服务器列表?
英文:
I am trying to edit an android vpn app that it's own server list.
i have a json of my own with server data but don't know how to embed it.
This is how my json response looks
[
[
"0",
"PL",
"Poland",
"warswaw",
"1.1.1.1",
"IKEv2",
"",
""
],
[
"1",
"AU",
"Australia",
"Sydney ",
"1.1.1.1",
"IKEv2",
"",
""
],
and this is the code in app
JSONObject json_response = new JSONObject("http://example.com/json.php");
JSONArray jsonArray = json_response.getJSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json_object = jsonArray.getJSONObject(i);
ServerArray[i][0] = json_object.getString("id");
;
ServerArray[i][1] = json_object.getString("file");
ServerArray[i][2] = json_object.getString("city");
ServerArray[i][3] = json_object.getString("country");
ServerArray[i][4] = json_object.getString("image");
ServerArray[i][5] = json_object.getString("ip");
ServerArray[i][6] = json_object.getString("active");
ServerArray[i][7] = json_object.getString("signal");
NumServers = NumServers + 1;
}
the arrays are from old api https://raw.githubusercontent.com/gayanvoice/android-vpn-client-ics-openvpn/images/appdetails.json
how can i rewrite so that it reads my server list ?
答案1
得分: 0
尝试使用Gson,这会更容易些。
英文:
Try to use Gson - it would be much easier for you.
答案2
得分: 0
你的JSON响应格式为字符串的数组数组。可能有更好的方式使用键值对来创建响应,但这取决于你。
你需要向你的端点发送请求以获取JSON响应。为了解析它,看看你的JSON响应的格式,并按以下方式解析:
JSONArray rootArray = response.getJSONArray(); // 获取根数组
for(int i = 0; i < rootArray.length(); i++){
JSONArray childArray = rootArray.getJSONArray(i); // 索引i处的子数组
for(int j = 0; j < childArray.length(); j++){
// 通过在childArray上使用getString方法遍历并获取字符串
}
}
英文:
Your JSON response is of the format Array of Arrays of Strings. There are probably better ways to create your response using key-value pairs, but that's up to you.
You have to send a request to your endpoint to get the JSON response. In order to parse it, look at the format of your JSON response, and parse like so:
JSONArray rootArray = response.getJSONArray(); // Gets the root array
for(int i = 0; i < rootArray.length(); i++){
JSONArray childArray = rootArray.getJSONArray(i); // child array at index i
for(int j = 0; j < childArray.length(); j++){
// Traverse through and get strings using getString method on childArray
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论