for (int i = 0; i < strings.size(); i++) { strings.set(i, strings.get(i).toUpperCase()); }
时间: 2024-06-01 11:13:21 浏览: 117
您的代码中有一个错误。在 for 循环中,变量 i 的递增部分被省略了,应该写成 i++ 或者 i+=1,否则代码会陷入死循环。修改后的代码如下:
for (int i = 0; i < strings.size(); i++) {
strings.set(i, strings.get(i).toUpperCase());
}
相关问题
c语言 检查一下下面的代码 为什么函数中获取不到键值#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/hmac.h> #include <jansson.h> #include <time.h> #include <errno.h> #include <resolv.h> #include <netdb.h> char* calculate_signature(char* json_str, char* key) { json_t *root; json_error_t error; /* 从文件中读取 JSON 数据 */ root = json_load_file(json_str, 0, &error); /* 遍历 JSON 对象中的所有键值对,并获取键的名称 */ int key_count = json_object_size(root); printf("key_names %d\n", key_count); const char *key_name; json_t *value; const char **key_names = (const char **)malloc(key_count * sizeof(char *)); int i = 0; json_object_foreach(root, key_name, value) { key_name = json_object_iter_key(value); key_names[i] = key_name; i++; } printf("key_names %s\n", key_names[2]); //int str_num = i; // 计算字符串数组中的字符串数量 /* char **sorted_names = sort_strings(key_names, key_count); char* stringA = (char*)malloc(1); // 初始化为一个空字符串 stringA[0] = '\0'; size_t len = 0; for (int i = 0; i < str_num; i++) { char* key = sorted_names[i]; json_t* value = json_object_get(root, key); char* str = json_dumps(value, JSON_ENCODE_ANY | JSON_COMPACT); len += strlen(key) + strlen(str) + 2; // 2 是键值对之间的字符 stringA = (char*)realloc(stringA, len); strcat(stringA, key); strcat(stringA, "="); strcat(stringA, str); strcat(stringA, "&"); free(str); } free(sorted_names); stringA[strlen(stringA) - 1] = '\0'; // 去掉最后一个"&" printf("stringA%s\n", stringA); unsigned char* sign = (unsigned char*)malloc(EVP_MAX_MD_SIZE); unsigned int sign_len = 0; HMAC(EVP_sha256(), key, strlen(key), (unsigned char*)stringA, strlen(stringA), sign, &sign_len); // 计算HMAC-SHA256签名 char* signature = (char*)malloc(sign_len * 2 + 1); // 签名的十六进制表示 signature[0] = '\0'; // 初始化为一个空字符串 for (int i = 0; i < sign_len; i++) { sprintf(signature + i * 2, "%02x", sign[i]); } json_object_set_new(root, "sign", json_string(signature)); // 在json中添加"sign"参数 json_dumpf(root, stdout, JSON_ENCODE_ANY | JSON_COMPACT); // 输出带有"sign"参数的json字符串 json_decref(root); free(key_names); free(stringA); free(sign); printf("signature%s\n", signature); */ return("A"); } int main() { char *key="39cabdfaab8c4da09bd6e9823c527836"; char *sss="{\"timestamp\":1685509898,\"sdkVersion\":\"1.0.30_1\",\"vin\":\"LJUBMSA24PKFFF198\"}"; calculate_signature(sss, key) ; }
代码中的问题在于 `json_load_file` 函数的第一个参数应该是一个文件名字符串,而不是 JSON 字符串。因此,需要将 `calculate_signature` 函数中的第一个参数 `json_str` 修改为文件名。
修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/hmac.h>
#include <jansson.h>
#include <time.h>
#include <errno.h>
#include <resolv.h>
#include <netdb.h>
char* calculate_signature(char* filename, char* key) {
json_t *root;
json_error_t error;
/* 从文件中读取 JSON 数据 */
root = json_load_file(filename, 0, &error);
if (!root) {
printf("Error in %s line %d: %s\n", error.source, error.line, error.text);
return NULL;
}
/* 遍历 JSON 对象中的所有键值对,并获取键的名称 */
int key_count = json_object_size(root);
printf("key_names %d\n", key_count);
const char *key_name;
json_t *value;
const char **key_names = (const char **)malloc(key_count * sizeof(char *));
int i = 0;
json_object_foreach(root, key_name, value) {
key_name = json_object_iter_key(value);
key_names[i] = key_name;
i++;
}
printf("key_names %s\n", key_names[2]);
//int str_num = i; // 计算字符串数组中的字符串数量
/*
char **sorted_names = sort_strings(key_names, key_count);
char* stringA = (char*)malloc(1); // 初始化为一个空字符串
stringA[0] = '\0';
size_t len = 0;
for (int i = 0; i < str_num; i++) {
char* key = sorted_names[i];
json_t* value = json_object_get(root, key);
char* str = json_dumps(value, JSON_ENCODE_ANY | JSON_COMPACT);
len += strlen(key) + strlen(str) + 2; // 2 是键值对之间的字符
stringA = (char*)realloc(stringA, len);
strcat(stringA, key);
strcat(stringA, "=");
strcat(stringA, str);
strcat(stringA, "&");
free(str);
}
free(sorted_names);
stringA[strlen(stringA) - 1] = '\0'; // 去掉最后一个"&"
printf("stringA%s\n", stringA);
unsigned char* sign = (unsigned char*)malloc(EVP_MAX_MD_SIZE);
unsigned int sign_len = 0;
HMAC(EVP_sha256(), key, strlen(key), (unsigned char*)stringA, strlen(stringA), sign, &sign_len); // 计算HMAC-SHA256签名
char* signature = (char*)malloc(sign_len * 2 + 1); // 签名的十六进制表示
signature[0] = '\0'; // 初始化为一个空字符串
for (int i = 0; i < sign_len; i++) {
sprintf(signature + i * 2, "%02x", sign[i]);
}
json_object_set_new(root, "sign", json_string(signature)); // 在json中添加"sign"参数
json_dumpf(root, stdout, JSON_ENCODE_ANY | JSON_COMPACT); // 输出带有"sign"参数的json字符串
json_decref(root);
free(key_names);
free(stringA);
free(sign);
printf("signature%s\n", signature);
*/
return("A");
}
int main() {
char *key="39cabdfaab8c4da09bd6e9823c527836";
char *filename="test.json";
calculate_signature(filename, key) ;
return 0;
}
```
另外,代码中还有一些注释掉的部分,需要先实现对应的函数才能使用。
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 JButton[] editButtons; // 修改按钮
private JButton[] deleteButtons; // 删除按钮
public InterfaceCallExample() {
super("Interface Call Example");
setSize(500, 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("更改时间");
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");
int rowCount = tableModel.getRowCount();
for (int i = 0; i < rowCount; i++) {
tableModel.removeRow(0);
}
editButtons = new JButton[list.size()];
deleteButtons = new JButton[list.size()];
for (int i = 0; i < list.size(); i++) {
Map<String,Object> map = list.get(i);
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<String>();
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);
// 添加修改按钮
editButtons[i] = new JButton("修改");
editButtons[i].addActionListener(new EditButtonActionListener(tableModel, i));
resultTable.setValueAt(editButtons[i], i, 5);
// 添加删除按钮
deleteButtons[i] = new JButton("删除");
deleteButtons[i].addActionListener(new DeleteButtonActionListener(tableModel, i));
resultTable.setValueAt(deleteButtons[i], i, 6);
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
// 修改按钮事件处理器
private class EditButtonActionListener implements ActionListener {
private DefaultTableModel tableModel;
private int row;
public EditButtonActionListener(DefaultTableModel tableModel, int row) {
this.tableModel = tableModel;
this.row = row;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 实现修改操作
System.out.println("修改第" + row + "行数据");
}
}
// 删除按钮事件处理器
private class DeleteButtonActionListener implements ActionListener {
private DefaultTableModel tableModel;
private int row;
public DeleteButtonActionListener(DefaultTableModel tableModel, int row) {
this.tableModel = tableModel;
this.row = row;
}
@Override
public void actionPerformed(ActionEvent e) {
tableModel.removeRow(row);
}
}
}
```
在上述代码中,我们添加了两个内部类 `EditButtonActionListener` 和 `DeleteButtonActionListener`,分别用于处理“修改”和“删除”按钮的事件。在 `actionPerformed` 方法中,我们将每一行的“修改”和“删除”按钮添加到表格中,同时为每个按钮添加相应的事件处理器。当用户单击“修改”或“删除”按钮时,相应的事件处理器会被触发,从而实现对数据的修改或删除。
阅读全文