我有一个包含以下数据的JSON文件:
{ "data" : [
{ "ID":"3b071d17-bfe5-4474-a7b4-58c755c7d954",
"value":"328.0"},
{ "ID":"dc4607f9-5955-4dd8-8c1a-abd3719edb6f",
"value":"764.1"},
{ "ID":"a4aa9f3b-599f-4815-5776-20fa38b064b5",
"value":"983.6"},
{ "ID":"c6fb7cd8-381d-93fa-711b-9482ab394ffa",
"value":"351.5"},
{ "ID":"2366a36b-8df2-72db-40bc-bbbe3258f09c",
"value":"539.3"}
]}
如何获取从ID dc4607f9-5955-4dd8-8c1a-abd3719edb6f (2nd)
到c6fb7cd8-381d-93fa-711b-9482ab394ffa (4th)
或最后数据的数据范围?是否可以这样做?
这是我的尝试:
List<float> dataSave = new ArrayList();
try {
JSONObject objectFromFile = ...; //JSONReadFile
JSONArray dataArray = objectFromFile.getJSONArray("data");
//here will get data from the start ID to end ID
dataSave.add((float)dataArray.getDouble("value");
} catch (JSONException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
如果我理解正确,你想要获取json数组的值以start id开头并停止在结束id上。两个限制都包括在内。
我很确定必须有一个更好的方法,但是这是我的例子,如果它可以帮助你:
convertJsonArrayToMap:从JsonArray
生成一个地图getValueBasedOnRange:保存id为startId的json对象的值,直到endId
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class App
{
static String data = "{ \"data\" : [\r\n" +
" { \"ID\":\"3b071d17-bfe5-4474-a7b4-58c755c7d954\",\r\n" +
" \"value\":\"328.0\"},\r\n" +
" { \"ID\":\"dc4607f9-5955-4dd8-8c1a-abd3719edb6f\",\r\n" +
" \"value\":\"764.1\"},\r\n" +
" { \"ID\":\"a4aa9f3b-599f-4815-5776-20fa38b064b5\",\r\n" +
" \"value\":\"983.6\"},\r\n" +
" { \"ID\":\"c6fb7cd8-381d-93fa-711b-9482ab394ffa\",\r\n" +
" \"value\":\"351.5\"},\r\n" +
" { \"ID\":\"2366a36b-8df2-72db-40bc-bbbe3258f09c\",\r\n" +
" \"value\":\"539.3\"}\r\n" +
"]}";
public static void main(String... args){
JsonParser jsonParser = new JsonParser();
JsonObject objectFromString = jsonParser.parse(data).getAsJsonObject();
JsonArray dataArray= objectFromString.getAsJsonArray("data");
//here will get data from the start ID to end ID
Set<Entry<String, Float>> dataMap = convertJsonArrayToMap(dataArray);
String startId = "dc4607f9-5955-4dd8-8c1a-abd3719edb6f";
String endId = "c6fb7cd8-381d-93fa-711b-9482ab394ffa";
List<Float> dataSave = getValueBasedOnRange(startId, endId,dataMap);
System.out.println(dataSave.toString());
}
//Generate and return the map from the JsonArray parameter
private static Set<Entry<String, Float>> convertJsonArrayToMap(JsonArray dataArray){
Map<String,Float> dataMap = new LinkedHashMap<>();
for (JsonElement currentElement : dataArray) {
JsonObject currentJsonObject = currentElement.getAsJsonObject();
dataMap.put(currentJsonObject.get("ID").getAsString(), currentJsonObject.get("value").getAsFloat());
}
return dataMap.entrySet();
}
//Generate and return the List<Float> from the map parameter
//Save the float value of the object with ID=startId
//Save the float value of the ANY object after startID
//Save the float value of the object with ID=endId and return
private static List<Float> getValueBasedOnRange(String startId, String endId, Set<Entry<String, Float>> dataMap){
boolean collectFlag = false;
List<Float> dataSave = new ArrayList<>();
for(Entry<String, Float> mapEntry : dataMap) {
if(startId.equals(mapEntry.getKey())) {
collectFlag = true;
}
if(collectFlag) {
dataSave.add(mapEntry.getValue());
}
if(endId.equals(mapEntry.getKey())) {
return dataSave;
}
}
return dataSave;
}
我希望它有所帮助。