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

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

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

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:

确定