2013-08-18 3 views
-1

내 레이아웃에 2 개의 TextView가 있습니다 (matricula, nome).이 값은 json request에서 가져와야합니다.URL에서 내용을 가져 와서 json을 구문 분석하는 방법

PHP

$alunos = json_decode("let's pretend json data is here"); 

echo "Matricula: " . $alunos['Aluno']['matricula']; 
echo "Nome: " . $alunos['Aluno']['nome']; 

jQuery를

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

console.log("Matricula: " + alunos.aluno.matricula); 
console.log("Nome: " + alunos.aluno.nome); 

을 :

내가 가져 오기 값으로 JSON 요청을 모두 difficults 이곳 내가 PHP와 JQuery와에서하는 방법을 예입니다 도움을 받으려면 :
Aluno = 학생
Matricula = 학생 신분
Nome = 이름

나는 json을 파싱하는 것에 대해 몇 가지 대답을 읽었지만 이해하기 어렵다. 또한 자바에서 쉽게

+0

PHP 또는 java를 통해 구문 분석해야합니까? –

+0

JAVA, PHP는 단지 예제 일뿐입니다. –

답변

1

은 (I 메인 흐름에 초점을 처리하는 모든 오류를 왼쪽, 자신의 것을 추가하십시오) : 자세한 내용은

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"); 

documentation를 참조하십시오.

+0

"theJsonString"은 무엇입니까? –

+0

당신의''json 데이터가 여기있다 ''와 같은 것 " – Henry

+0

하지만 자바에서 json을 구문 분석하는 방법을 모른다. –