Java JSON 解析教程:简单易懂的示例
可以使用Java中内置的json库来解析json。以下是一个简单的例子:
import org.json.*;
String jsonString = '{"name":"John", "age":30, "city":"New York"}';
JSONObject json = new JSONObject(jsonString);
String name = json.getString('name');
int age = json.getInt('age');
String city = json.getString('city');
System.out.println('Name: ' + name);
System.out.println('Age: ' + age);
System.out.println('City: ' + city);
输出结果为:
Name: John
Age: 30
City: New York
在这个例子中,我们首先创建了一个包含json数据的字符串。然后,我们使用JSONObject类从字符串中创建了一个json对象。接着,我们使用getString和getInt方法从json对象中获取相应的数据。最后,我们将这些数据打印出来。
原文地址: https://www.cveoy.top/t/topic/n9M1 著作权归作者所有。请勿转载和采集!