[英]How do I get data from float[], JSONArray parsing?
I get a response in the JSONArray form which looks like this: 我得到了JSONArray形式的响应,如下所示:
"["37:0.00",
"60:23.50:55.00:22.22:52.00:192.55:20.00"
"61:0.00"]";
In these 3 rows the first numbers (37, 60, 61) are IDs (type int
). 在这3行中,第一个数字(37、60、61)是ID(类型int
)。 The other numbers for these specific rows are values (type float
). 这些特定行的其他数字是值(类型float
)。 How do I get these values for a specific ID to float[]
? 如何获取特定ID的这些值以float[]
?
Here's my code: 这是我的代码:
public void setValuesFromResponse(String response) {
deviceList = database.getDevices();
List<float[]> parsedData = parse(response);
Log.i("setValFromResp: ", "parsedData --> DONE");
for (int i = 0; i < parsedData.size(); i++) {
int parsedDeviceId = (int) parsedData.get(i)[0]; //get ID
float[] values = parsedData.get(i)[1];
//how to check if there is another value in another index (for this exmaple index 2
}
}
Here is the parse method: 这是解析方法:
public List<float[]> parse(String json)
{
List<float[]> list = new ArrayList<>();
try {
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
String dataByDevice = array.getString(i);
String[] values = dataByDevice.split(":");
list.add(new float[values.length]);
for (int j = 0; j < values.length; j++) {
list.get(i)[j] = Float.parseFloat(values[j]);
}
}
return list;
}
Thanks for your help. 谢谢你的帮助。
"//how to check if there is another value in another index (for this exmaple index 2" you can add another inner loop to iterate over the array. “ //如何检查另一个索引中是否存在另一个值(对于此示例索引2”,您可以添加另一个内部循环来遍历数组。
for (int i = 0; i < parsedData.size(); i++) {
int parsedDeviceId = (int) parsedData.get(i)[0]; //get ID
float[] values = parsedData.get(i)[1];
//how to check if there is another value in another index (for this exmaple index 2
}
Like this: 像这样:
for (int i = 0; i < parsedData.size(); i++) {
int parsedDeviceId = (int) parsedData.get(i)[0]; //get ID
for(int j = 1; j < parsedData.get(i).length; j++)
{
//process parsedData.get(i)[j] here
}
}
Since you have all data including id
in one array, you can simply skip id
to get values. 由于您将所有包含id
数据都包含在一个数组中,因此只需跳过id
即可获取值。
public List<float[]> parse(String json) {
List<float[]> list = new ArrayList<>();
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
String dataByDevice = array.getString(i);
String[] values = dataByDevice.split(":");
list.add(new float[values.length]);
for (int j = 0; j < values.length; j++) {
list.get(i)[j] = Float.parseFloat(values[j]);
}
}
return list;
}
public void setValuesFromResponse(String response) {
List<float[]> parsedData = parse(response);
for(int i = 0; i < parsedData.size(); ++i) {
int parsedDeviceId = (int) parsedData.get(i)[0]; // get ID
System.out.print(parsedDeviceId + " : ");
for(int j = 0; j < parsedData.get(i).length; ++j) {
System.out.print(parsedData.get(i)[j] + ", ");
}
System.out.println();
}
}
or you can create a copy of array and then iterate through it 或者您可以创建数组的副本,然后遍历它
for(int i = 0; i < parsedData.size(); ++i) {
int parsedDeviceId = (int) parsedData.get(i)[0]; // get ID
float[] data = Arrays.copyOfRange(parsedData.get(i), 1, parsedData.get(i).length);
System.out.print(parsedDeviceId + " : ");
for(int j = 0; j < data.length; ++j) {
System.out.print(data[j] + ", ");
}
System.out.println();
}
Also, this is example of how to use Map
, I think this is more appropriate in your situation. 另外,这是如何使用Map
示例,我认为这在您的情况下更合适。
public Map<Integer, float[]> parse(String json) {
Map<Integer, float[]> list = new HashMap<>();
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
String dataByDevice = array.getString(i);
String[] values = dataByDevice.split(":");
/* first value is a key */
Integer id = Integer.parseInt(values[0]);
list.put(id, new float[values.length - 1]);
/* others are values */
for (int j = 1; j < values.length - 1; j++) {
list.get(id)[j] = Float.parseFloat(values[j]);
}
}
return list;
}
public void setValuesFromResponse(String response) {
Map<Integer, float[]> parsedData = parse(response);
for(Entry<Integer, float[]> entry : parsedData.entrySet()) {
int parsedDeviceId = entry.getKey(); // get ID
System.out.print(parsedDeviceId + " : ");
for(float value : entry.getValue()) {
System.out.print(value + ", ");
}
System.out.println();
}
}
In all cases output for 在所有情况下,输出
String response = "[\"37:0.00\",\"60:23.50:55.00:22.22:52.00:192.55:20.00\",\"61:0.00\"]";
will be 将会
37 : 0.0,
60 : 23.5, 55.0, 22.22, 52.0, 192.55, 20.0,
61 : 0.0,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.