2016-05-25 11 views
0

すでに多くのタイプのソリューションを検索しましたが、それを変更してエラーを解決する方法はまだわかりません。私はアンドロイドで非常に新しいので、私は誰かの助けが必要です。ここでJSONObjectをJSONArrayに変換できません4

org.json.JSONException: Value {"user_info":[{"MemberID":"1","Name":"Joshua","Url":"[email protected]"},{"MemberID":"2","Name":"Mary","Url":"[email protected]"}]} of type org.json.JSONObject cannot be converted to JSONArray 

私の変数とJSON-ファイルを解析するコードです:

パブリッククラスMainActivityアクティビティ{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    TextView id = (TextView) findViewById(R.id.id); 
    TextView name = (TextView) findViewById(R.id.name); 
    TextView url = (TextView) findViewById(R.id.url); 

    JSONObject json = null; 
    String str = ""; 
    HttpResponse response; 
    HttpClient myClient = new DefaultHttpClient(); 
    HttpPost user_info = new HttpPost("http://192.168.1.103/ViewList/getJSON.php"); 

    try { 
     response = myClient.execute(user_info); 
     str = EntityUtils.toString(response.getEntity(), "UTF-8"); 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try{ 
     JSONArray jArray = new JSONArray(str); 
     json = jArray.getJSONObject(0); 

     id.setText(json.getString("MemberID")); 
     name.setText(json.getString("Name")); 
     url.setText(json.getString("Url")); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

答えて

0

コードを変更して、JSON文字列をJSONOBjectと最初に読み取ってから、"user_info"JSONArrayと読んでください。ここでの変更は以下のとおりです。

try{ 
     JSONObject jsonObject = new JSONObject(str); 
     //then here you read user_info as array 
     JSONArray jArray = jsonObject.getJSONArray("user_info"); 
     json = jArray.getJSONObject(0); 

     id.setText(json.getString("MemberID")); 
     name.setText(json.getString("Name")); 
     url.setText(json.getString("Url")); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

は試してみて、それはあなたの問題を解決するかどうか、私たちに知らせていることを与えます。

+0

あなたの提案に感謝し、最後に私に働いています。 – Ziha

+0

優れています。これがあなたを助けてくれてうれしいです。ハッピーコーディング。 – ishmaelMakitla

0

あなたの問題はあなたがJSONArrayオブジェクトを供給しているということですが拡張文字列はJSONObjectで始まります。クラスJSONObjectはまた、あなたが使用する必要がある方法getJSONArrayあります

JSONObject jObject = new JSONObject(str); 
JSONArray jArray = jObject.getJSONArray("user_info"); 

をそして、あなたはjArray以上のループをすることができます。

+0

バリデータを使ってOPの文字列を調べていたので、実際にJSON文字列が配列として始まるインスタンスがあるのでしょうか?私はそれが{[?これは可能ですか?その場合は 'JSONArray jArray = new JSONArray(str);'を使用します。 –

+0

いいえ、他の例も無効です。 JSONのオブジェクトには常にキーがあります。そして、[]で始まる配列を送信するだけの有効なJSONです。 – tolgap

+0

最後の文は私には分かりませんが、JSONは[で始まり、もちろん配列になりますか? –

関連する問題