主要活动
public class MainActivity extends Activity {
String param;
String Result;
TextView tv1,tv2;
String TAG = "myApp";
String strParsedValue = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postData(param);
try {
parseJSON();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
映射文本视图
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
}
public void postData(final String param) {
RequestQueue rq = Volley.newRequestQueue(this);
StringRequest postReq = new StringRequest(Request.Method.POST,
"http://api.docx.json",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// tv.setText(response); // We set the response data in
// the TextView
/* Toast.makeText(getApplicationContext(),
"Response" + response, Toast.LENGTH_LONG).show();*/
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error [" + error + "]");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("key", "value);
params.put("cal", "com");
params.put("a", "x");
return params;
}
};
rq.add(postReq);
}
private void parseJSON() throws JSONException {
String json = "";
JSONObject jsonObject = new JSONObject(json);
JSONArray columns = jsonObject.getJSONArray("columns");
String column0 = columns.getString(0);
String column1 = columns.getString(1);
JSONArray rowsWrapper = jsonObject.getJSONArray("rows");
JSONArray rows = rowsWrapper.getJSONArray(0);
int row0 = rows.getInt(0);
String row1 = rows.getString(1);
Log.d("values-", "column0 " + column0 + " column1 " + column1+ " row0 " + row0 + " row1 " + row1);
Toast.makeText(getApplicationContext(), ""+columns, Toast.LENGTH_LONG).show();
}
已使用键值对
{ &#34;列&#34;:[ &#34;可比&#34 ;, &#34; Com描述&#34; ] &#34;行&#34;:[ [ 20614, &#34; RAX&#34; ] ] }
以上是json
答案 0 :(得分:0)
如果你对这样的POJO进行建模,那就容易多了:
public class MyPOJO {
private String[] columns;
private String[][] rows;
public String[] getColumns() {
return columns;
}
public void setColumns(String[] columns) {
this.columns = columns;
}
public String[][] getRows() {
return rows;
}
public void setRows(String[][] rows) {
this.rows = rows;
}
@Override
public String toString() {
return "ClassPojo [columns = " + columns + ", rows = " + rows + "]";
}
}
然后你这样做:
Gson gson=new Gson();
MyPOJO p = gson.fromJson(YOUR_JSON_STRING, MyPOJO.class);