2012-07-16 148 views
5

我是JSON和REST的新手。我与返回一个字符串像这样的一台服务器的工作:REST和JSON - 将字符串转换为JSON数组

[{"username":"Hello","email":"[email protected]","credits":"100","twitter_username":""},{"username":"Goodbye","email":"[email protected]","credits":"0","twitter_username":""}] 

我已经成功地打印出来作为在控制台上琴弦,但现在我想将它们转换成JSON阵列。我到目前为止的代码没有返回任何错误,但我不知道要为新的JSON数组添加构造函数。我一直在提及一位同事发送给我的一段代码,其中构造函数是新的JSONArray(响应),但他从未告诉我“响应”是什么。

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:[email protected]/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(); 

     } 

    } 
} 

回答

11
  • 使用GSON字符串格式化为JsonArray
  • 然后遍历JsonArray得到的值

的代码示例

String json = "[{\"username\":\"Hello\",\"email\":\"[email protected]\",\"credits\":\"100\",\"twitter_username\":\"\"},{\"username\":\"Goodbye\",\"email\":\"[email protected]\",\"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("*********"); 
} 
+0

谢谢!像魅力一样工作 – Tiffany 2012-07-16 13:49:43

+0

我确实有一个问题 - 有没有办法让json字符串不必像这样硬编码? – Tiffany 2012-07-16 13:55:48

+1

尝试在我给出的代码示例中的getJson函数作为答案,在下面的帖子中:http://stackoverflow.com/questions/11471884/need-help-android-json-currency-converter/11472677#11472677。使用它,您可以从外部服务器下载json数据 – sunil 2012-07-16 16:21:14

1

我正在使用gson库来操纵json。你可以从here下载gson。这是一个处理json的非常好的库。 创建JSON解析器首先,它将解析JSON字符串:

JsonParser parser = new JsonParser(); 

现在初始化一个空的JSON数组

JsonArray jArray = new JsonArray(); 

现在使用的解析器创建JSON数组

jArray = parser.parse(outputString).getAsJsonArray(); 
0

我有使用org.json.simple.JSONObject和org.json.simple.JSONArray,我希望net.sf.json.JSONArray也可以工作。

你应该把下面的字符串格式为输出(JSONArray阵列=新JSONArray(输出);)

{"YOUR_ARRAY_NAME": [{"username":"Hello","email":"[email protected]","credits":"100","twitter_username":""},{"username":"Goodbye","email":"[email protected]","credits":"0","twitter_username":""}]} 

现在这是JSONArray的字符串表示。