2013-07-23 2 views
-1

나는 이것을 시도해 보았고 그 밑으로 갈 수 없었다. 난 그냥 '제목'을 원하는안드로이드에서 PHP에서 JSON 추출하기

[{"post":{"title":"Property Litigation Assistant Solicitor"}},{"post":{"title":"Trusts and Probate Practitioner"}}] 

다음은 PHP 스크립트에서 JSON 출력 내 코드는 다음

HttpResponse response = httpclient.execute(httppost); 
    String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 
    JSONObject object = new JSONObject(jsonResult); 

    //JSONArray innerobj = object.getJSONArray("post"); 
    String name = object.getString("title"); 
    //String name = name1.getString(0); 

    Log.v("isurgeon", name); 

    //String name = object.getString("posts"); 
    //String verion = object.getString("version"); 
    textView.setText(name + " - "); 

을 니펫을

$query = "SELECT `title` FROM `c2torsdb`.`pE_vacancies` WHERE active=1 LIMIT 2"; 
$result = mysql_query($query,$link) or die('Errant query: '.$query); 
/* create one master array of the records */ 
$posts = array(); 
if(mysql_num_rows($result)) { 
while($post = mysql_fetch_assoc($result)) { 
$posts[] = array('post'=>$post); 
    } 
    } 
    /* output in necessary format */ 
    if($format == 'json') { 
    header('Content-type: application/json, charset=utf-8'); 
    echo json_encode($posts); 
    } 

그리고 여기가 PHP 코드를입니다.

+1

무엇이 문제입니까? 크래시 (로그 고양이주세요), 또는 무엇? 단지 추측 : JSON 응답은 배열이므로 '새 JSONObject (jsonResult)'는 작동하지 않습니다. 대신 새로운 JSONArray (jsonResult)를 사용하십시오. – SimonSays

답변

0

솔직히 말해서 나는 안드로이드에 익숙하지 않지만 JSON을 알고 있습니다. 나는 다음과 같은 자바 스크립트로 JSON 출력을하여 배치 인 경우 :

var abc = [{"post":{"title":"Property Litigation Assistant Solicitor"}},{"post":{"title":"Trusts and Probate Practitioner"}}] 

나는이 일을하여 첫번째 타이틀을 얻을 것입니다 :

abc[0].title 

당신은 반환의 n 번째 요소를 선택해야합니다을 json object 그 다음 제목을 얻습니다.

0

빌의 권리 - 당신의 JSON이 출력되는 방식으로, 모든 것이 배열로 묶여

안드로이드 IMO JSON 개체에 대한 정말 좋은 기본 처리가/배열 그래서 당신은 단순히

JSONArray jsonArray = new JSONArray(jsonResult); 

JSONObject object = jsonArray[0]; 

모든 것을 할 경우 제대로 작동해야합니다.