我正在尝试替换routeName和routeDurationInMinutes但是rpelace方法不起作用。当我在运行功能之外使用它时它正在工作,但它内部不起作用。有任何想法吗?我添加了我想要运行的所有代码。对不起,这是如此来回。我试图缩短它以便其他人阅读。
The Value g = {"routes":[{"routeName":"I-190 N; Electric Ave","routeDurationInMinutes":70,"routeLengthKM":83.865,"routeLengthMiles":52.111278915,"toll":false},{"routeName":"I-190 N; Greenville Rd","routeDurationInMinutes":82,"routeLengthKM":92.569,"routeLengthMiles":57.519692099000004,"toll":false}],"startPoint":"street address","endPoint":"destination","startLatitude":"42.20115054203528","startLongitude":"-71.85038140607858","endLatitude":"42.201220801535","endLongitude":"-71.849075146695"}
final Runnable rundatapoll = new Runnable() {
@Override
public void run() {
String g;
try {
g = new apicall().execute().get().getBody().toString();
System.out.println(g);
String route1;
String route2;
String time1;
String time2;
String time3;
ArrayList parse= new ArrayList();
ArrayList route= new ArrayList();
ArrayList time= new ArrayList();
Pattern p= Pattern.compile("routeName?.+?routeLengthKM");
Matcher m = p.matcher(g);
Pattern p2= Pattern.compile("routeName?.+?routeDurationInMinutes");
Pattern p3= Pattern.compile("routeDurationInMinutes?.+?routeLengthKM");
Matcher m3= p3.matcher(g);
while (m.find()) {
parse.add(m.group());
}
while (m3.find()){
time.add(m3.group());
}
int l=0;
while (l<parse.size()){
Matcher m2 =p2.matcher((CharSequence) parse.get(l));
while(m2.find()){
route.add(m2.group());
}
l++;
}
//////////////////////////////////////////////////////////////////////////////////////
route1= (String) route.get(0);
route2= (String) route.get(1);
route1= route1.replace(",routeDurationInMinutes","");
route1= route1.replace("routeName:","");
route2= route2.replace(",routeDurationInMinutes","");
route2= route2.replace("routeName:","");
time1= (String) time.get(0);
time2= (String) time.get(1);
time1= time1.replace("routeDurationInMinutes:","");
time1= time1.replace(",routeLengthKM","");
time2= time2.replace("routeDurationInMinutes:","");
time2= time2.replace(",routeLengthKM","");
t1.setValue(time1);
t2.setValue(time2);
r1.setValue(route1);
r2.setValue(route2);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
};
>>>routeName":"I-190 N; Electric Ave","routeDurationInMinutes
答案 0 :(得分:1)
看起来线程可能没有按照评论中的建议执行。
这对我有用:
package example;
public class Example {
final String g="routeName:I-190 N; Electric Ave,routeDurationInMinutes";
final Runnable rundatapoll = new Runnable() {
@Override
public void run() {
String route1=g;
System.out.println("BEFORE THREAD");
System.out.println(route1);
route1= route1.replace(",routeDurationInMinutes","");
route1= route1.replace("routeName:","");
System.out.println("AFTER RUNNING THREAD:");
System.out.println(route1);
System.out.println("DONE");
}
};
public static void main(String[] args) {
Example example = new Example();
Thread thread = new Thread(example.rundatapoll);
thread.start();
}
}
输出:
BEFORE THREAD
routeName:I-190 N; Electric Ave,routeDurationInMinutes
AFTER RUNNING THREAD:
I-190 N; Electric Ave
DONE
答案 1 :(得分:0)
或者您可以使用JSON解析器。像这样:
package example;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JsonExample {
public static void main(String[] args) {
try {
String json = "{\"routes\":[{\"routeName\":\"I-190 N; Electric Ave\",\"routeDurationInMinutes\":70,\"routeLengthKM\":83.865,\"routeLengthMiles\":52.111278915,\"toll\":false},{\"routeName\":\"I-190 N; Greenville Rd\",\"routeDurationInMinutes\":82,\"routeLengthKM\":92.569,\"routeLengthMiles\":57.519692099000004,\"toll\":false}],\"startPoint\":\"street address\",\"endPoint\":\"destination\",\"startLatitude\":\"42.20115054203528\",\"startLongitude\":\"-71.85038140607858\",\"endLatitude\":\"42.201220801535\",\"endLongitude\":\"-71.849075146695\"}";
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(json);
JSONArray routes = (JSONArray) obj.get("routes");
System.out.println("ROUTES: ");
for(Object route : routes) {
JSONObject routeObj = (JSONObject) route;
String routeName = routeObj.get("routeName") + "";
System.out.println("Route name: " + routeName);
}
} catch (Exception exp) {
throw new RuntimeException(exp);
}
}
}
输出:
ROUTES:
Route name: I-190 N; Electric Ave
Route name: I-190 N; Greenville Rd