0

我有一个来自 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
4

1 回答 1

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。

于 2013-05-08T10:23:57.413 回答