方法一

@Slf4j
public class test {
public static void main(String[] args) {
String json = "[\"偏好零售\",\"偏好金融\",\"偏好教育\"]";
JSONArray objects = null;
try {
objects = JSONObject.parseArray(json);
} catch (Exception e) {
log.info("e:{}", e);
}
String s = objects.toString();
String replace = s.replace("[", "");
String replace1 = replace.replace("\"", "");
String replace2 = replace1.replace("]", "");
System.out.println(replace2);

}
}

方法二

@Slf4j
public class test {
public static void main(String[] args) {
String json = "[\"偏好零售\",\"偏好金融\",\"偏好教育\"]";
JSONArray objects = JSONArray.parseArray(json);
StringBuilder sb = new StringBuilder();
for (Object object : objects) {
sb.append(object + ",");
}
sb = sb.delete(sb.length() - 1, sb.length());
System.out.println(sb);


// JSONArray objects = null;
// try {
// objects = JSONObject.parseArray(json);
// } catch (Exception e) {
// log.info("e:{}", e);
// }
// String s = objects.toString();
// String replace = s.replace("[", "");
// String replace1 = replace.replace("\"", "");
// String replace2 = replace1.replace("]", "");
// System.out.println(replace2);

}
}

最后都是得到

偏好零售,偏好金融,偏好教育

补充:对象转json
JSON.toJSONString(product)

public class MetadataTest {
public static void main(String[] args) {
KeyValue keyValue = new KeyValue();
keyValue.setKey("reportInstanceId");
keyValue.setValue("219836400005213");
String jsonString = JSON.toJSONString(keyValue);
System.out.println(jsonString);

String reportId = "219836400005213";
String s = JSON.toJSONString(reportId);
System.out.println("===" + s);
}
}

java接收json数组转成字符串、对象转json、json字符串转list_json字符串


3///json字符串转list

String paramScores = "[\n" +
" {\n" +
" \"key\": 1,\n" +
" \"valueType\": 1,\n" +
" \"value\": \"100\",\n" +
" \"score\": 6\n" +
" },\n" +
" {\n" +
" \"key\": 2,\n" +
" \"valueType\": 3,\n" +
" \"value\": \"10-20\",\n" +
" \"score\": 1\n" +
" }\n" +
"]";
List<ParamScore> paramScore = JSONArray.parseArray(paramScores, ParamScore.class);
System.out.println(paramScore);

其他方法

java接收json数组转成字符串、对象转json、json字符串转list_java_02


Shop shop = JSONUtil.toBean(shopJson, Shop.class);

反过来 JSONUtil.toJsonStr(shop);