如果JSON数据在字符串
中,如何从以下JSON读取数据{
"name": "test",
"values": [
{
"valu": "23",
"valu1": "24",
"valu2": "25"
}
]
}
答案 0 :(得分:2)
使用Gson和Gson#fromJson
方法之一
YourType o = new Gson().fromJson("your json string", YourType.class);
YourType o = new Gson().fromJson(new StringReader("your json string"),
YourType.class);
简单示例:
定义要将json数据映射到的类:
static class Hobby {
Hobby(String n) { name = n; }
String name;
@Override
public String toString() {
return "Hobby [name=" + name + "]";
}
}
static class Person {
String firstName, lastName;
int age;
List<Hobby> hobbies = new ArrayList<Hobby>();
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ ", age=" + age + ", hobbies=" + hobbies + "]";
}
}
现在使用以下Json字符串
进行测试@Test
public void fromJson() {
Person o = new Gson().fromJson(
"{\r\n" +
" \"firstName\":\"John\",\r\n" +
" \"lastName\":\"Doe\",\r\n" +
" \"age\":24,\r\n" +
" \"hobbies\":[\r\n" +
" {\r\n" +
" \"name\":\"Programming\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"name\":\"Sports\"\r\n" +
" }\r\n" +
" ]\r\n" +
"}",
Person.class);
System.out.println(o.toString());
}
打印:
Person [firstName=John, lastName=Doe, age=24, hobbies=[Hobby [name=Programming], Hobby [name=Sports]]]
请参阅类Gson的javadoc以获取其他替代方案。另请查看他们的user-guide
答案 1 :(得分:1)
你需要一些像Jackson这样的库来为你的解析。另见Jackson tutorial。我认为从tree model开始是最容易的。
答案 2 :(得分:1)
try {
String data = null;
String url = "localhost:8080/test/rest/Action/xyz";
String[] dataArray = null;
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest getRequest = new HttpGet(getUrl);
getRequest.addHeader("User-Agent", USER_AGENT);
getRequest.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("key",
"pwd"), "UTF-8", false));
getRequest.setHeader("Content-Type", "application/json");
HttpResponse response = client.execute(getRequest);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
data = rd.readLine();
}
} catch () {
}
try {
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
data = EntityUtils.toString(response.getEntity());
dataArray = data.split(",");
}
catch (Exception ex) {
}
JSONObject mJSONObject = new JSONObject(data);
try {
JSONArray mjSONArray = mJSONObject.getJSONArray("values");
for (int i = 0; i < mjSONArray.length(); i++) {
JSONObject obj;
obj = mjSONArray.getJSONObject(i);
valu = obj.getString("valu");
valu1= obj.getString("valu1");
valu2= obj.getString("valu2");
system.out.println(valu);
system.out.println(valu1);
system.out.println(valu2);
}
使用此方法,您可以获取变量中的所有jsonarray数据。
答案 3 :(得分:0)
有许多JSON解析器。 我发现json-simple很容易实现。 一组decoding-examples显示如何使用String
创建JSON-Object这是一个从String读取JSON对象的代码片段。 (直接从上面的解码示例页面链接获取此代码段。)
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=JSONValue.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=JSONValue.parse(s);
System.out.println(obj);
s="[5,]";
obj=JSONValue.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=JSONValue.parse(s);
System.out.println(obj);
JSONObject是java.util.Map,JSONArray是java.util.List,因此您可以使用Map或List的标准操作访问它们