@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CollectApplication.class)
public class JsonTest {
@Test
public void test() {
/**
* [{\"key\":1, \"keyValue\":\"自定义公式\",\"dataKey\":\"complete_design\",\"dataText\":\"建筑面积\",
* \"operationType\":\"4\",\"value\":\"95\",\"isNumber\":\"2\",\"isChooseBool\":\"true\"},
* {\"key\":2,\"keyValue\":\"具体金额\"}]
*/
String dataValue = "[{\"key\":1, \"keyValue\":\"自定义公式\",\"dataKey\":\"complete_design\","
+ "\"dataText\":\"建筑面积\",\"operationType\":\"4\",\"value\":\"95\",\"isNumber\":\"2\","
+ "\"isChooseBool\":\"false\"},{\"key\":2,\"keyValue\":\"具体金额\",\"isChooseBool\":\"true\"}]";
List<JSONObject> jsonObjects = JSON.parseArray(dataValue, JSONObject.class);
for (JSONObject jsonObject : jsonObjects) {
int key = jsonObject.getIntValue("key");
System.out.println("========" + key);
if (Objects.equals(ShopTemplateDataConstant.FIRST_KEY, key)) {
}
}
}
}
还有一种使用google封装的Gson
Gson gson = new Gson();
HashMap map = gson.fromJson(accessTokenTnfo, HashMap.class);
String key = (String)map.get("key");