Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从url获取json并解析信息_Android_Json - Fatal编程技术网

Android 从url获取json并解析信息

Android 从url获取json并解析信息,android,json,Android,Json,我需要从json中获取这两个信息“matricula”和“nome”,但我不知道如何做到这一点。我想使用是个不错的选择,但我不知道如何使用从url获取json的方法,也不知道在获取json后如何使用数组键。很抱歉,我要求您完成全部工作,但这是因为我是android开发新手,我对jsonarray、JsonObject等非常困惑。JsonObject在Java中类似: 它存储具有关联值的密钥 例如: JsonObject obj = new JsonObject(); JsonObject obj

我需要从json中获取这两个信息“matricula”和“nome”,但我不知道如何做到这一点。我想使用是个不错的选择,但我不知道如何使用从url获取json的方法,也不知道在获取json后如何使用数组键。很抱歉,我要求您完成全部工作,但这是因为我是android开发新手,我对jsonarray、JsonObject等非常困惑。

JsonObject在Java中类似:

它存储具有关联值的密钥

例如:

JsonObject obj = new JsonObject();
JsonObject obj1 = new JsonObject();
obj1.put("obj1_key1","obj1_value1");
obj1.put("obj1_key2","obj1_value2");
obj1.put("obj1_key3","obj1_value3");
obj.put("obj1",obj1);
JsonArray array = new JsonArray();
JsonObject obj  = new JsonObject();
obj.put("xxx","yyy");
array.add(obj);
JsonArray就像Java中的

它存储索引为0到N的值

例如:

JsonObject obj = new JsonObject();
JsonObject obj1 = new JsonObject();
obj1.put("obj1_key1","obj1_value1");
obj1.put("obj1_key2","obj1_value2");
obj1.put("obj1_key3","obj1_value3");
obj.put("obj1",obj1);
JsonArray array = new JsonArray();
JsonObject obj  = new JsonObject();
obj.put("xxx","yyy");
array.add(obj);
当您想要检索json字符串时,请记住,当数组以
[]
开头时,您需要使用JsonArray来解析它,当数组以
{}
开头时,您需要使用JsonObject

你可以看到一个例子

您的JSON

{"matricula":"201110460","nome":"Daniel de Faria Pedro"}
这只是一个JsonObject,因为它具有经典的“映射”样式的键值关联。 您需要做的是:

try {
    URL url = new URL("http://tcc-teste.aws.af.cm/api/get/aluno/1");
    URLConnection connection = url.openConnection();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line = "", json = "";
    while((line = reader.readLine())!=null){
        json+=line;
    }                    
    JsonObject yourObject = new JsonObject(json);
    String matricula = yourObject.get("matricula");
    String nome = yourObject.get("nome");
    System.out.println(nome+" - "+matricula);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}
我会这样做:

假设您从一个包含两列(matricula和nome)的数据库中获取信息,我想您会发送一个数组,因此我将使用此代码构建该数组

 $json['test'][]=array( 'matricula'  => $row["matricula"], 'nome' =>$row["nome"])
然后我会像这样解析这些信息,假设您知道如何获取这些信息:

 JSONObject object = new JSONObject(jsonResult);
 JSONArray test = object.getJSONArray("test");
 for(int i = 0; i < test.length(); i++){
     JSONObject c = test.getJSONObject(i);
     String matricula= c.getString("matricula");
     String nome = c.getString("nome");
     //do whatever you want with it
 }
JSONObject object=新的JSONObject(jsonResult);
JSONArray test=object.getJSONArray(“test”);
对于(int i=0;i
您可以看看我写的这个示例/教程,它是关于使用本机android
JSONObject
JSONArray
对象或使用
Gson
库解析
JSON
文件的:


您尝试了什么?什么都没有,因为我不知道如何在java上处理jsonarray和jsonobject,所以这是这个问题的开始。试试这个Jackson库,它可以通过几行代码从InputStream解码您的实体:对不起,您能给我一个基于我问题url中json数据结构的示例吗?假设我已经从url获取了数据。这个php数组是什么意思?您可以同时从数据库发送更多行。这就是JSONArray的工作方式,之后可以进行回显。