REST和JSON - 将字符串转换为JSON数组 [英] REST and JSON - converting string to JSON array
本文介绍了REST和JSON - 将字符串转换为JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是JSON和REST的新手。我正在使用一个返回如下字符串的服务器:
I'm new to JSON and REST. I'm working with a server that returns strings like these:
[{"username":"Hello","email":"hello@email.com","credits":"100","twitter_username":""},{"username":"Goodbye","email":"goodbye@email.com","credits":"0","twitter_username":""}]
我设法将它们打印出来控制台上的字符串,但现在我想将它们转换为JSON数组。到目前为止我的代码没有返回任何错误,但我不知道将什么放入新JSON数组的构造函数中。我一直指的是一位同事发给我的一段代码,其中构造函数是新的JSONArray(响应),但他从未告诉我'回应'是什么。
I've managed to print them out as strings on the console, but now I want to convert them into a JSON array. The code I have so far returns no errors, but I don't know what to put into the constructor for the new JSON array. I've been referring to a piece of code sent to me by a colleague, in which the constructor was new JSONArray(response) but he never told me what 'response' was.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import sun.misc.BASE64Encoder;
public class NetClientGet {
public static void main(String[] args) {
try {
URL url = new URL("http://username:password@mobile.crowdedmedia.co.uk/index.php/api/users/get_users/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = "username:password";
String encoded = enc.encode(userpassword.getBytes());
conn.setRequestProperty("Authorization", "Basic " + encoded);
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
JSONArray array = new JSONArray(output);
for (int i =0; i < array.size(); i++) {
JSONObject row = array.getJSONObject(i);
String user = row.getString("username");
System.out.println(user);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐答案
- 使用 GSON 将字符串格式化为JsonArray
- 然后遍历JsonArray以获取值
- use GSON to format the string as JsonArray
- then traverse the JsonArray to get the values
代码示例
String json = "[{\"username\":\"Hello\",\"email\":\"hello@email.com\",\"credits\":\"100\",\"twitter_username\":\"\"},{\"username\":\"Goodbye\",\"email\":\"goodbye@email.com\",\"credits\":\"0\",\"twitter_username\":\"\"}]";
JsonArray jArray = new JsonParser().parse(json).getAsJsonArray();
for (int i=0;i<jArray.size();i++) {
JsonObject jsonObject = jArray.get(i).getAsJsonObject();
System.out.println(jsonObject.get("username"));
System.out.println(jsonObject.get("email"));
System.out.println(jsonObject.get("credits"));
System.out.println(jsonObject.get("twitter_username"));
System.out.println("*********");
}
这篇关于REST和JSON - 将字符串转换为JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文