我如何用其他人替换java多个字符和字符串;

时间:2013-05-08 09:53:04

标签: android string str-replace

我有一个来自http请求的字符串,我想用其他人替换多个字符和字符串。我怎么能这样做?使用Array来提高效率?

      String result =" "hourly": [ {"cloudcover": "0", "humidity": "93", "precipMM": "0.0", "pressure": "1013", "sigHeight_m": "0.7", "swellDir": "70", "swellHeight_m": "0.5", "swellPeriod_secs": "1.0", "tempC": "9", "tempF": "48", "time": "0", "v";
      String result2 = result.replace("{", " ");
      String result3 = result2.replace("}", " ");
      String result4 = result3.replace("[", " ");
      String result5 = result4.replace("]", " ");
      String result6 = result5.replace("\"", "");

      String result7 = result6.replaceAll("......", "         ");
      String result8 = result7.replaceAll("cloudcover", "\n      \ncloudcover");
      String result9 = result8.replaceAll("winddir:", "    \nwinddir:");
      String result10 = result9.replaceAll("tempC:", "    \ntempC:");

    WeatherInfos.setText( result10 );//Shows the weather info

1 个答案:

答案 0 :(得分:0)

尝试以下

String result = "{\"hourly\": [ {\"cloudcover\": \"15\", \"humidity\": \"93\", \"pressure\": \"1013\", \"tempC\": \"9\", \"winddir\": \"25\"}]}";

if(!result.startsWith("{"))
    result = "{" + result + "}";

JSONObject JSONResult = new JSONObject(result);
JSONResult = (JSONObject) JSONResult.getJSONArray("hourly").get(0);

String cloudcover =  JSONResult.getString("cloudcover");
String tempC=  JSONResult.getString("tempC");
String winddir=  JSONResult.getString("winddir"); //i do not see winddir in your result

Toast.makeText(getapplicationContext(), "Cloud Cover is "+ cloudcover , Toast.LENGTH_LONG).show();

现在打印cloudcover,winddir和tempC,无论你想要什么。