Android 如何从URL获取内容并解析json

Android 如何从URL获取内容并解析json,android,json,android-parser,Android,Json,Android Parser,我有2个文本视图在我的布局与id的(matricula,nome),我需要得到这个值从这个 在生成json请求和获取值这两方面我都有困难,下面是一个我在php和jquery中应该如何做的示例: PHP Jquery var alunos = $.parseJSON("let's pretend json data is here"); console.log("Matricula: " + alunos.aluno.matricula); console.log("Nome: " + alun

我有2个文本视图在我的布局与id的(matricula,nome),我需要得到这个值从这个

在生成json请求和获取值这两方面我都有困难,下面是一个我在php和jquery中应该如何做的示例:

PHP

Jquery

var alunos = $.parseJSON("let's pretend json data is here");

console.log("Matricula: " + alunos.aluno.matricula);
console.log("Nome: " + alunos.aluno.nome);
帮助:
Aluno=学生
Matricula=学生id
Nome=名称

我在这里读到了一些关于解析json的答案,但我承认,这很难理解。

在Java中也很容易(我忽略了所有错误处理以关注主流,请自己补充):


有关详细信息,请参见。

您需要通过php或java解析它?java,php只是一个示例“theJsonString”是什么?与您的
相同“让我们假设json数据在这里”
我已经编辑了答案现在更清楚了吗?解析是在您创建JSONObject时完成的。我理解,但您必须假设我不知道如何从我拥有的url中提取“{Aluno:{“Matri..}}”,所以实际上问题不是解析JSON,而是从url获取内容?从您发布的问题中根本不清楚这一点。
var alunos = $.parseJSON("let's pretend json data is here");

console.log("Matricula: " + alunos.aluno.matricula);
console.log("Nome: " + alunos.aluno.nome);
import org.json.JSONObject;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.InputStreamReader;

...

private String readString(Reader r) throws IOException {
    char[] buffer = new char[4096];
    StringBuilder sb = new StringBuilder(1024);
    int len;
    while ((len = r.read(buffer)) > 0) {
        sb.append(buffer, 0, len);
    }
    return sb.toString();
}

...

// fetch the content from the URL
URL url = new URL("http://..."); // add URL here
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream(), "UTF-8");
String jsonString = readString(in);
in.close();
conn.disconnect();

// parse it and extract values
JSONObject student = new JSONObject(jsonString);
String id = student.getJSONObject("Aluno").getString("matricula");
String name = student.getJSONObject("Aluno").getString("nome");