如何访问此 Java 对象中的这些属性

huangapple go评论97阅读模式
英文:

How to have access to those properties in this Java object

问题

以下是翻译好的部分:

  1. {"Data1": 4,"data2":array of Strings,"Data3":"String","data4": integer}
  2. 我已经将其从JSONArray解析为Object类型因为API的响应是一个对象数组现在我需要访问属性值以便能够测试它们并在其他类中使用它们
  3. 到目前为止编写的代码
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. try {
  7. URL url = new URL ("www.example.com/");
  8. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  9. BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  10. String json = bf.readLine();
  11. JSONArray ja = new JSONArray(json);
  12. System.out.println(ja);
  13. for (Object type:ja ) {
  14. //System.out.println(type);
  15. Type t = new Type(type);
  16. //System.out.println(t.getName());
  17. }
  18. } catch (MalformedURLException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }
英文:

{"Data1": 4,"data2":array of Strings,"Data3":"String","data4": integer}

I have parsed it from a JSONArray to the type Object as the response from the API is a an array of objects , now I need to access the properties values in order to be able to test them and use them in other classes
The code written so far

public static void main(String[] args) {
// TODO Auto-generated method stub

  1. try {
  2. URL url = new URL ("www.example.com/");
  3. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  4. BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  5. String json = bf.readLine();
  6. JSONArray ja = new JSONArray(json);
  7. System.out.println(ja);
  8. for (Object type:ja ) {
  9. //System.out.println(type);
  10. Type t = new Type(type);
  11. //System.out.println(t.getName());
  12. }
  13. } catch (MalformedURLException e) {
  14. // TODO Auto-generated catch block
  15. e.printStackTrace();
  16. } catch (IOException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. }

答案1

得分: 0

  1. try {
  2. URL url = new URL("www.example.com/");
  3. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  4. BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  5. String json = bf.readLine();
  6. JSONArray ja = new JSONArray(json);
  7. System.out.println(ja);
  8. int n = 0;
  9. for (Object type : ja) {
  10. JSONObject object = type.getJSONObject(n++);
  11. String prop = object.getString("propertyname");
  12. }
  13. } catch (MalformedURLException e) {
  14. // TODO Auto-generated catch block
  15. e.printStackTrace();
  16. } catch (IOException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. }
  21. try this
英文:
  1. try {
  2. URL url = new URL ("www.example.com/");
  3. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  4. BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  5. String json = bf.readLine();
  6. JSONArray ja = new JSONArray(json);
  7. System.out.println(ja);
  8. int n=0;
  9. for (Object type:ja ) {
  10. JSONObject object = type.getJSONObject(n++);
  11. String prop=object.getString("propertyname");
  12. }
  13. } catch (MalformedURLException e) {
  14. // TODO Auto-generated catch block
  15. e.printStackTrace();
  16. } catch (IOException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. }
  21. try this

huangapple
  • 本文由 发表于 2020年10月1日 19:34:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64154565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定