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

Android 如何用其他字符和字符串替换java多个字符和字符串;,android,string,str-replace,Android,String,Str Replace,我有一个来自http请求的字符串,我想用其他字符和字符串替换多个字符和字符串。我如何做到这一点?用数组来实现更高效的方法 String result =" "hourly": [ {"cloudcover": "0", "humidity": "93", "precipMM": "0.0", "pressure": "1013", "sigHeight_m": "0.7", "swellDir": "70", "swellHeight_m": "0.5", "swellPeriod_

我有一个来自http请求的字符串,我想用其他字符和字符串替换多个字符和字符串。我如何做到这一点?用数组来实现更高效的方法

      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
试着跟随

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。

我认为可以在json中响应,因此不需要仅使用json替换。如果需要,则替换字符串。您可以使用
regex
!这是您试图操作的JSON响应吗?发布您试图解析的http链接。如果是JSON,为什么不解析JSON对象/数组?好的。告诉我一个教程或JSON数组替换字符串的示例…我如何在winddir:25之后获得信息?例如“tempC:19”我想将其转换为每小时19摄氏度:[{“cloudcover”:“0”,“湿度”:“93”,“precipMM”:“0.0”,“压力”:“1013”,“sigHeight_m”:“0.7”,“swellDir”:“70”,“swellHeight_m”:“0.5”,“swellPeriod_secs”:“1.0”,“tempC”:“9”,“tempF”:“48”,“time”:“0”,“vIn JSON”您可以直接获取要查找的字符串的值,例如JSONResult.getString(“cloudcover”);将返回值“0”cloudcover的。因此,您不需要运行替换字符串。您只需获取所需的值即可使用我编辑的代码中的结果字符串,然后重试。我们需要在初始化的字符串中使用\“if want use”。