从没有Array的ObjectJson获取信息

时间:2017-12-05 11:20:56

标签: android json

我有下面的课程,我可以用这种格式从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:

{“name”:“Bruno”}

我发现了一个类似于How to parse JSON Object Android Studio的问题,但我无法将其应用到我的示例中。

2 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

尝试使用此功能,您应该将aJsonResponse替换为来自服务器的响应

import { InAppBrowser, InAppBrowserOptions, InAppBrowserObject } from '@ionic-native/in-app-browser';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {    
  public browser: InAppBrowserObject;    
  constructor(public navCtrl: NavController, private inAppBrowser: InAppBrowser,) {  }

  start() {    
    const options: InAppBrowserOptions = {
      hidden: 'no',
      closebuttoncaption: 'Close',
      clearcache: 'yes',
      clearsessioncache: 'yes',
      zoom: 'no',
      location: 'yes'
    };   

    this.browser = this.inAppBrowser.create('http://test.com', '_blank', options);
    this.browser.show();

    this.browser.on('loaderror').subscribe(loadStopData => {
      this.log('loaderror');
    });

    this.browser.on('loadstop').subscribe(loadStopData => {          
      this.browser.executeScript({
        code:
          "var elemCl1 = document.querySelector('#partWelcome'); "        
      })
      .then( data => {    
          console.log('loadstop');  <-- never arises
      })
      .catch(e => {
            console.log('EXC 11', e);
      })        
...   

    });
  };