英文:
How to have access to those properties in this Java object
问题
以下是翻译好的部分:
{"Data1": 4,"data2":array of Strings,"Data3":"String","data4": integer}
我已经将其从JSONArray解析为Object类型,因为API的响应是一个对象数组,现在我需要访问属性值以便能够测试它们并在其他类中使用它们。
到目前为止编写的代码
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
URL url = new URL ("www.example.com/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String json = bf.readLine();
JSONArray ja = new JSONArray(json);
System.out.println(ja);
for (Object type:ja ) {
//System.out.println(type);
Type t = new Type(type);
//System.out.println(t.getName());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
英文:
{"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
try {
URL url = new URL ("www.example.com/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String json = bf.readLine();
JSONArray ja = new JSONArray(json);
System.out.println(ja);
for (Object type:ja ) {
//System.out.println(type);
Type t = new Type(type);
//System.out.println(t.getName());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案1
得分: 0
try {
URL url = new URL("www.example.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String json = bf.readLine();
JSONArray ja = new JSONArray(json);
System.out.println(ja);
int n = 0;
for (Object type : ja) {
JSONObject object = type.getJSONObject(n++);
String prop = object.getString("propertyname");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try this
英文:
try {
URL url = new URL ("www.example.com/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String json = bf.readLine();
JSONArray ja = new JSONArray(json);
System.out.println(ja);
int n=0;
for (Object type:ja ) {
JSONObject object = type.getJSONObject(n++);
String prop=object.getString("propertyname");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论