0

我将 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");
4

3 回答 3

0

当您调用时,putExtra("name",jsonArray.optJSONObject(position).toString());您将整个 JSON 对象存储为变量“名称”下的字符串,该变量将被解析为新的 Activity。当您从 Intent 中获取“名称”时,它就是返回的整个 JSON 对象。

如果您只想发送名称而不是整个字符串,请尝试使用此方法jsonArray.optJSONObject(position).getString("name")而不是使用该toString()方法

于 2013-07-01T06:48:01.253 回答
0

JSON 连载 -呸!- 你可以很容易地将它作为额外的任何活动传递给任何活动。

于 2013-07-01T06:48:26.063 回答
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;

}
}

使用这个获取一个 JSON 对象,您可以轻松解析它以获得必要的值!

如果 json 对象中有许多其他对象jsonArray,则将其放入循环并解析。

于 2013-07-01T06:49:44.303 回答