我将json数组作为参数传递给其他活动但是当我点击时显示文本中的所有值,在星期一看到这个图像
http://postimg.org/image/fbjlm8y97/91beaa03/
显示在http://postimg.org/image/8m0f1bjaf/987e8eb4/之类的其他活动上
字符串中的所有内容如何在TextView中仅显示名称?现在每件事我怎么解析json数组???
{
"student": [
{
"id": 1,
"name": "Monday",
"dish": "Biryani",
"Gender": "M",
"age": 10,
"birthdate": "23/05/2002"
},
{
"id": 2,
"name": "Tuesday",
"dish": "Sandwish",
"Gender": "M",
"age": 12,
"birthdate": "08/01/2000"
},
{
"id": 3,
"name": "Wednesday",
"dish": "Chicken Tikka",
"Gender": "F",
"age": 14,
"birthdate": "01/03/1998"
},
代码:
try {
JSONObject mainJson = new JSONObject(result);
jsonArray = mainJson.getJSONArray(ARRAY_NAME);
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
Item objItem = new Item();
objItem.setId(objJson.getInt(ID));
objItem.setName(objJson.getString(NAME));
myname= objJson.getString(NAME);
objItem.setCity(objJson.getString(CITY));
objItem.setGender(objJson.getString(GENDER));
objItem.setAge(objJson.getInt(AGE));
objItem.setBirthdate(objJson.getString(BIRTH_DATE));
arrayOfList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
setAdapterToListview();
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(!jsonArray.isNull(position)){
Intent intent = new Intent(SeletecDayofweek.this,TodayLunch.class);
intent.putExtra("name", jsonArray.optJSONObject(position).toString());
startActivity(intent);
}
}
and get like this
Intent intent = getIntent();
String stringRecd = intent.getStringExtra("name");
答案 0 :(得分:0)
当您调用putExtra("name",jsonArray.optJSONObject(position).toString());
时,您将整个JSON对象存储为变量“name”下的字符串,该变量将被解析为新的Activity。当您从Intent获取“name”时,它将返回整个JSON对象。
如果您只想发送名称而不是整个字符串,请尝试使用jsonArray.optJSONObject(position).getString("name")
而不是toString()
方法
答案 1 :(得分:0)
JSON序列化 - boing! - 您可以非常轻松地将其作为额外内容传递给任何活动。
答案 2 :(得分:0)
写一个JSONParser
课程:
public class JSONParser
{
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser()
{
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(
String url ,
String method ,
List<NameValuePair> params )
{
Log.d( "JSON" , "making HTTP request" );
// Making HTTP request
try
{
// check for request method
if (method == "POST")
{
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost( url );
httpPost.setEntity( new UrlEncodedFormEntity( params ) );
HttpResponse httpResponse = httpClient.execute( httpPost );
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else
if (method == "GET")
{
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString =
URLEncodedUtils.format( params , "utf-8" );
url += "?" + paramString;
HttpGet httpGet = new HttpGet( url );
HttpResponse httpResponse = httpClient.execute( httpGet );
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
BufferedReader reader =
new BufferedReader( new InputStreamReader(
is ,
"iso-8859-1" ) , 8 );
StringBuilder sb = new StringBuilder();
String line = null;
while ( ( line = reader.readLine() ) != null)
{
sb.append( line + "\n" );
}
is.close();
json = sb.toString();
}
catch (Exception e)
{
Log.e( "Buffer Error" , "Error converting result " + e.toString() );
}
// try parse the string to a JSON object
try
{
jObj = new JSONObject( json );
}
catch (JSONException e)
{
Log.e( "JSON Parser" , "Error parsing data " + e.toString() );
}
// return JSON String
return jObj;
}
}
使用此fetch可以轻松解析JSON对象以获取必要的值!
如果json对象中有许多其他jsonArray
,则将其放入循环并解析。