java - 解析没有数组名称的 Json 数组
问题描述
LineGraphSeries<DataPoint> series;
ArrayList<HashMap<String, String>> arrayListData;
public static String data;
TextView graphOneTV, graphTwoTV, graphThreeTV;
GraphView graph;
String x, y;
public Graph_Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_graph_, container, false);
}
HttpURLConnection connection;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
graphOneTV = (TextView) getView().findViewById(R.id.TVGraph1);
graphTwoTV = (TextView) getView().findViewById(R.id.TVGraph2);
graphThreeTV = (TextView) getView().findViewById(R.id.TVGraph3);
graph = (GraphView) getView().findViewById(R.id.graph1);
//graphDataClass gdc = new graphDataClass();
//gdc.execute();
String strJson="{ \"Employee\" :[{\"id\":\"101\",\"name\":\"Sonoo Jaiswal\"," +
"\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"Vimal Jaiswal\",\"salary\":\"60000\"}] }";
String data = "";
try {
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("id").toString());
String name = jsonObject.optString("name").toString();
float salary = Float.parseFloat(jsonObject.optString("salary").toString());
data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";
}
graphOneTV.setText(data);
} catch (JSONException e) {e.printStackTrace();}}}
所以我试图通过使用上面的代码来解析 JSON 数组并且它工作但后来我试图从我的本地主机 pc 中获取 JSON,但是 JSON 没有数组名称。下面给出了我试图获取的 json。
[{"twitter":"200","googleplus":"60"},{"twitter":"150","googleplus":"180"},{"twitter":"90","googleplus":"120"}]
下面给出了获取和解析这个 JSON 数组的代码。
LineGraphSeries<DataPoint> series;
ArrayList<HashMap<String, String>> arrayListData;
public static String data,data2;
TextView graphOneTV, graphTwoTV, graphThreeTV;
GraphView graph;
//String x, y;
public Graph_Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_graph_, container, false);
}
HttpURLConnection connection;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
graphOneTV = (TextView) getView().findViewById(R.id.TVGraph1);
graphTwoTV = (TextView) getView().findViewById(R.id.TVGraph2);
graphThreeTV = (TextView) getView().findViewById(R.id.TVGraph3);
graph = (GraphView) getView().findViewById(R.id.graph1);
graphDataClass gdc = new graphDataClass();
gdc.execute();
/*String strJson="{ \"Employee\" :[{\"id\":\"101\",\"name\":\"Sonoo Jaiswal\"," +
"\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"Vimal Jaiswal\",\"salary\":\"60000\"}] }";
String data = "";
try {
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("id").toString());
String name = jsonObject.optString("name").toString();
float salary = Float.parseFloat(jsonObject.optString("salary").toString());
data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";
}
graphOneTV.setText(data);
} catch (JSONException e) {e.printStackTrace();}
*/
}
public class graphDataClass extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL("http://192.168.1.34/getGraphData.php?");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data = data + line;
}
JSONObject jsonRootObject = new JSONObject(data);
JSONArray jsonArray = jsonRootObject.optJSONArray("");
for(int i=0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int x = Integer.parseInt(jsonObject.optString("twitter").toString());
int y = Integer.parseInt(jsonObject.optString("googleplus").toString());
data2 += "Node" + i + " : \n id= " + x + " \n Name= " + y ;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPreExecute() {
super.onPreExecute();
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
graphOneTV.setText(data2);
}
}
}
但是如前所述,这个 JSON 没有数组名,所以我应该在需要数组名的地方写什么。
JSONArray jsonArray = jsonRootObject.optJSONArray("我在这里写什么节目????(JSON 中没有数组名称!!!");
解决方案
修改php代码文件
<?php
if($connect){
die('Could Not Connect :'.mysqli_error());
}
$result = mysqli_query($connect,"SELECT * FROM table_name);
while($row=mysqli_fetch_assoc($result)){
$output[]=$row;
}
print(json_encode(array('yourarrayname' =>$output),JSON_PRETTY_PRINT));
mysqli_close($connect);
?>
JSONArray jsonArray = jsonRootObject.optJSONArray("yourarrayname_define_php_json_file");
推荐阅读
- windows - 添加 DNS“A”记录,但缺少 TTL(生存时间)设置(Windows Server 2019)
- python - 如何在另一个小部件的函数中使用 Tkinter 'wait_variable' 小部件方法来等待其他两个按钮之一被单击?
- flutter - Flutter - flutter_inappwebview - GL 错误:内存不足
- r - Sparklyr - 无法连接到“本地”
- c# - 插入数据库时出现无效的对象名称“主要”错误 - C# (WebForms)、MySql
- .net - 在 NLog 中使用多个参数将对象打印为 JSON
- java - "? extends Stream " 和 "Stream "的区别
- javascript - 将 tailwind css 构建为公共站点并将 vuetify 构建为管理仪表板
- python - 将字符串从 K 转换为数千等,并删除 pyspark 数据帧中字符串之间的空格
- java - 如何定期使用多线程获取服务器状态