OthInfo(index=8, colName=PLMN, columnValue=null, indexs=null, colNames=null, colValues=null, lessLength=null, sourceClomnLength=null)转为对象
时间: 2023-07-06 19:26:42 浏览: 90
NR网络拒绝码-cause_value = 17 (0x11) (Network failure).docx
这个看起来像是一个自定义的Java类的toString()方法的输出结果,如果想要将其转换为Java对象,需要根据类的定义来进行解析。
假设OthInfo类的定义如下:
```java
public class OthInfo {
private int index;
private String colName;
private String columnValue;
private List<Integer> indexs;
private List<String> colNames;
private List<String> colValues;
private Integer lessLength;
private Integer sourceClomnLength;
// 构造函数、getter和setter方法等
// ...
@Override
public String toString() {
return "OthInfo(index=" + index + ", colName=" + colName + ", columnValue=" + columnValue
+ ", indexs=" + indexs + ", colNames=" + colNames + ", colValues=" + colValues
+ ", lessLength=" + lessLength + ", sourceClomnLength=" + sourceClomnLength + ")";
}
}
```
那么,我们可以编写一个工具方法,将上述字符串转换为OthInfo对象,示例代码如下:
```java
public static OthInfo parseOthInfo(String str) {
OthInfo othInfo = new OthInfo();
String[] fields = str.replace("OthInfo(", "").replace(")", "").split(", ");
othInfo.setIndex(Integer.parseInt(fields[0].split("=")[1]));
othInfo.setColName(fields[1].split("=")[1]);
othInfo.setColumnValue(fields[2].split("=")[1]);
if (!"null".equals(fields[3].split("=")[1])) {
List<Integer> indexs = new ArrayList<>();
String[] indexArr = fields[3].split("=")[1].split(",");
for (String indexStr : indexArr) {
indexs.add(Integer.parseInt(indexStr));
}
othInfo.setIndexs(indexs);
}
if (!"null".equals(fields[4].split("=")[1])) {
List<String> colNames = Arrays.asList(fields[4].split("=")[1].split(","));
othInfo.setColNames(colNames);
}
if (!"null".equals(fields[5].split("=")[1])) {
List<String> colValues = Arrays.asList(fields[5].split("=")[1].split(","));
othInfo.setColValues(colValues);
}
if (!"null".equals(fields[6].split("=")[1])) {
othInfo.setLessLength(Integer.parseInt(fields[6].split("=")[1]));
}
if (!"null".equals(fields[7].split("=")[1])) {
othInfo.setSourceClomnLength(Integer.parseInt(fields[7].split("=")[1]));
}
return othInfo;
}
```
调用示例:
```java
String str = "OthInfo(index=8, colName=PLMN, columnValue=null, indexs=null, colNames=null, colValues=null, lessLength=null, sourceClomnLength=null)";
OthInfo othInfo = parseOthInfo(str);
System.out.println(othInfo.getIndex()); // 输出 8
System.out.println(othInfo.getColName()); // 输出 PLMN
System.out.println(othInfo.getColumnValue()); // 输出 null
// ...
```
需要注意的是,这里只是一个简单的示例,如果输入的字符串格式不正确,或者字段值包含了非法字符,还需要加入更多的异常处理逻辑。
阅读全文