JSONObject category1 = new JSONObject();
时间: 2024-06-17 21:04:02 浏览: 11
JSONObject是Java中一个常用的JSON数据格式解析库,用于对JSON字符串进行解析、生成和操作等。在这里,category1是一个JSONObject对象的实例化,它可以存储键值对,用于描述一些数据信息。通过该对象,可以使用put方法将需要存储的数据以键值对的方式存入category1对象中。例如,下面的代码展示了如何创建一个名为category1的JSONObject对象,并在其中存储了三个键值对:
```
JSONObject category1 = new JSONObject();
category1.put("id", 1);
category1.put("name", "Java");
category1.put("description", "Java is a programming language");
```
在这个例子中,我们创建了一个category1对象,并向其中添加了三个键值对。其中,id键对应的值是1,name键对应的值是"Java",description键对应的值是"Java is a programming language"。这样,我们就可以通过这个category1对象来获取或者修改这些键值对的值。
相关问题
public class InterfaceCallExample extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton callButton; private JTable resultTable; private DefaultTableModel tableModel; public InterfaceCallExample() { super("Interface Call Example"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); callButton = new JButton("查看数据"); callButton.addActionListener(this); panel.add(callButton); add(panel, BorderLayout.NORTH); tableModel = new DefaultTableModel(); tableModel.addColumn("id"); tableModel.addColumn("材料类型"); tableModel.addColumn("状态"); tableModel.addColumn("创建时间"); tableModel.addColumn("更改时间"); resultTable = new JTable(tableModel); add(new JScrollPane(resultTable), BorderLayout.CENTER); } public static void main(String[] args) { InterfaceCallExample example = new InterfaceCallExample(); example.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == callButton) { try { URL url = new URL("http://101.34.120.182:8081/wnapp/productType/getTypeId?category=1&status=0&type="); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String s = reader.readLine(); JSONObject jsonObject = new JSONObject(); JSONObject object = JSONObject.parseObject(s); Object o = object.get("data"); JSONObject object1 = JSONObject.parseObject(o.toString()); ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) object1.get("list"); for (Map<String,Object> map : list){ String id = map.get("id").toString(); String titleName = map.get("titleName").toString(); String status = map.get("status").toString(); String createTime = map.get("createTime").toString(); String updateTime = map.get("updateTime").toString(); ArrayList<String> strings = new ArrayList<>(); strings.add(id); strings.add(titleName); strings.add(status); strings.add(createTime); strings.add(updateTime); String[] data = strings.toString().replace("[","").replace("]","").split(","); tableModel.addRow(data); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } 改成可以根据输入的条件进行查询
要实现根据输入的条件进行查询,你需要在界面中添加输入框和按钮,并在按钮的点击事件中获取输入框中的内容,并将其作为请求参数发送给后端接口。
下面是修改后的代码示例:
```
public class InterfaceCallExample extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton callButton;
private JTable resultTable;
private DefaultTableModel tableModel;
private JTextField categoryField, statusField, typeField;
public InterfaceCallExample() {
super("Interface Call Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
callButton = new JButton("查看数据");
callButton.addActionListener(this);
panel.add(callButton);
JLabel categoryLabel = new JLabel("材料类型:");
categoryField = new JTextField(10);
JLabel statusLabel = new JLabel("状态:");
statusField = new JTextField(10);
JLabel typeLabel = new JLabel("类型:");
typeField = new JTextField(10);
panel.add(categoryLabel);
panel.add(categoryField);
panel.add(statusLabel);
panel.add(statusField);
panel.add(typeLabel);
panel.add(typeField);
add(panel, BorderLayout.NORTH);
tableModel = new DefaultTableModel();
tableModel.addColumn("id");
tableModel.addColumn("材料类型");
tableModel.addColumn("状态");
tableModel.addColumn("创建时间");
tableModel.addColumn("更改时间");
resultTable = new JTable(tableModel);
add(new JScrollPane(resultTable), BorderLayout.CENTER);
}
public static void main(String[] args) {
InterfaceCallExample example = new InterfaceCallExample();
example.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == callButton) {
try {
String category = categoryField.getText();
String status = statusField.getText();
String type = typeField.getText();
String urlStr = "http://101.34.120.182:8081/wnapp/productType/getTypeId?";
if (!category.isEmpty()) {
urlStr += "category=" + URLEncoder.encode(category, "UTF-8") + "&";
}
if (!status.isEmpty()) {
urlStr += "status=" + URLEncoder.encode(status, "UTF-8") + "&";
}
if (!type.isEmpty()) {
urlStr += "type=" + URLEncoder.encode(type, "UTF-8") + "&";
}
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String s = reader.readLine();
JSONObject jsonObject = new JSONObject();
JSONObject object = JSONObject.parseObject(s);
Object o = object.get("data");
JSONObject object1 = JSONObject.parseObject(o.toString());
ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) object1.get("list");
for (Map<String,Object> map : list){
String id = map.get("id").toString();
String titleName = map.get("titleName").toString();
String statusStr = map.get("status").toString();
String createTime = map.get("createTime").toString();
String updateTime = map.get("updateTime").toString();
ArrayList<String> strings = new ArrayList<>();
strings.add(id);
strings.add(titleName);
strings.add(statusStr);
strings.add(createTime);
strings.add(updateTime);
String[] data = strings.toString().replace("[","").replace("]","").split(",");
tableModel.addRow(data);
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
```
修改后的代码中,我们添加了三个文本框和对应的标签,用于输入查询条件。在按钮的点击事件中,我们获取这些文本框中的内容,并将其作为请求参数发送给后端接口。如果某个文本框为空,则不包含对应的查询条件。注意,我们还使用了 `URLEncoder.encode()` 方法对参数进行编码,以避免传输过程中出现空格等特殊字符导致的错误。
解释如下代码:public static List<String> t2(JSONObject json) { List<String> result = new ArrayList<>(); String success = json.getStr("success"); JSONObject jsonObject = json.getJSONObject("result"); JSONArray data = jsonObject.getJSONArray("data"); data.forEach(temp -> { JSONObject obj = (JSONObject)temp; String uniquekey = obj.getStr("uniquekey"); String title = obj.getStr("title"); String date = obj.getStr("date"); String category = obj.getStr("category"); String url = obj.getStr("url"); System.out.println(url); System.out.println(title); System.out.println(category); System.out.println(date); System.out.println(uniquekey); result.add(uniquekey); }); return result; }
这段代码定义了一个静态方法t2,它接收一个JSONObject对象作为参数。该方法首先创建一个空的ArrayList对象result用于存储后续处理得到的数据。接着从传入的JSONObject对象中获取一个名为success的字符串变量,并将其赋值给success变量。接下来从该对象中获取名为result的JSONObject对象,并将其赋值给jsonObject变量。然后从jsonObject中获取名为data的JSONArray数组对象,并将其赋值给data变量。接着对data数组中的每个元素执行forEach循环处理,将每个元素转换为JSONObject对象并获取其uniquekey、title、date、category和url等5个属性的值,并将这些值逐一输出到控制台上。最后将uniquekey值添加到result列表中,并最终返回该列表。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)