2017-12-05 4 views
-5

なしObjectJsonから情報を取得し、私は以下のクラスを持っていると私は、この形式でJSONArrayから情報を取得することができる:アレイ

{「cliente」:[{「ID」:「1334」、「ノーム」 : "ブルーノ"}]}

TextView nome_usuario; 

private static final String TAG_CLIENTE = "cliente"; 
private static final String TAG_NOME = "nome"; 

JSONArray cliente = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) { 
View grafico = inflater.inflate(R.layout.fragment_fragment_perfil, container, false); 
nome_usuario = (TextView) grafico.findViewById(R.id.txtNome); 
new JSONParse().execute(); 
return grafico; 
} 


private class JSONParse extends AsyncTask<String, String, JSONObject> { 
private ProgressDialog pDialog; 
@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    pDialog = new ProgressDialog(getActivity()); 
    pDialog.setMessage("Atualizando"); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(true); 
    pDialog.show(); 

} 

@Override 
protected JSONObject doInBackground(String... args) { 
    JSONParser jParser = new JSONParser(); 
     JSONObject json = jParser.getJSONFromUrl("url do json"); 
     return json; 

} 
@Override 
protected void onPostExecute(JSONObject json) { 
    pDialog.dismiss(); 
    try { 
     cliente = json.getJSONArray(TAG_CLIENTE); 
     JSONObject c = cliente.getJSONObject(0); 
     String nome = c.getString(TAG_NOME); 
     nome_usuario.setText(nome); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


} 
} 

} 

しかし、今、私は次の形式でJSONと協力したいと思います:

{ "名": "ブルーノ"}

私はHow to parse JSON Object Android Studioと似た質問を見つけましたが、私の例には適用できませんでした。

+0

からの応答でaJsonResponseを交換する必要がありますあなたの質問は何ですか? –

+0

2番目のjsonで情報を検索する方法は、配列にNULLを回答として持たないためです –

+0

どちらの情報を取得しますか?キー 'name'の値は必要ですか? –

答えて

0

はJSONObjectから情報を取得する:

JSONObject aJsonObject = new JSONObject(aJsonResponse); 
JSONArray aJsonArray = aJsonObject.getJSONArray("cliente"); 
for (int i = 0; i < aJsonArray.length(); i++) { 
    aJsonObject = aJsonArray.getJSONObject(i); 
String aId = aJsonObject .getString("id"); 
String aNome = aJsonObject .getString("nome"); 
} 
0

は、これを使用してみてください、あなたはサーバー

JSONObject aJsonObject = new JSONObject(aJsonResponse); 

String aId = aJsonObject.getString("id"); 
String aNome = aJsonObject.getString("name");