How to get text from JSON without [“ ”]

2019-08-30 01:30发布

How do I get text from json without [" "] only text, in Android project?

this is my json from url {"code":200,"lang":"en-ru","text":["Better late than never"]}

i need get text "text":["Better late than never"] without [" "] only text: Better late than never

myclass MAINACTIVITY

public class MainActivity extends Activity {
    JSONParser jsonparser = new JSONParser();
    TextView tv;
    String ab;
    JSONObject jobj = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tvResult);
        new retrievedata().execute();

    }

    class retrievedata extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            jobj = jsonparser.makeHttpRequest("https://translate.yandex.net/api/v1.5/tr.json/translate?key=YOURAPIKEY&text=Better%20late%20than%20never&lang=ru");

            // check your log for json response
            Log.d("Login attempt", jobj.toString());

            ab = jobj.optString("text");
            return ab;
        }
        protected void onPostExecute(String ab){

            tv.setText(ab);
        }

    }

}

MY JSONPARSER CLASS

public class JSONParser {

    static InputStream is = null;
    static JSONObject jobj = null;
    static String json = "";
    public JSONParser(){

    }
    public JSONObject makeHttpRequest(String url){
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            try {
                HttpResponse httpresponse = httpclient.execute(httppost);
                HttpEntity httpentity = httpresponse.getEntity();
                is = httpentity.getContent();

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                try {
                    while((line = reader.readLine())!=null){
                        sb.append(line+"\n");   

                    }
                    is.close();
                    json = sb.toString();
                    try {
                        jobj = new JSONObject(json);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {

                    e.printStackTrace();
                }

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return jobj;

    }

}

2条回答
Luminary・发光体
2楼-- · 2019-08-30 01:45

From your json from url {"code":200,"lang":"en-ru","text":["Better late than never"]} Try this...

Yout JSON structure

{
  "code": 200,
  "lang": "en-ru",
  "text": [
    "Better late than never"
   ]
}

You can get your output using below..

try {
    JSONObject jsonObj = new JSONObject(json);
    String code = jsonObj.getString("code");
    String lang = jsonObj.getString("lang");
    JSONArray text = jsonObj.getJSONArray("text");
    Log.e("output", "code:" + code + "\nlang:" + lang + "\ntext"
                    + text.getString(0));
    } catch (Exception e) {
    e.printStackTrace();
    }
查看更多
beautiful°
3楼-- · 2019-08-30 01:47

This is a problem with people who aren't that familiar with JSON structure. When I am looking at JSON, I find it easier to pretty-print it, because it makes the structure very clear. In the case of your JSON: {"code":200,"lang":"en-ru","text":["Better late than never"]}, it becomes:

{
  "code": 200,
  "lang": "en-ru",
  "text": [
    "Better late than never"
   ]
}

The error in your code is that you are trying to parse the "text" key of the dictionary as a String, when it is instead, an array containing a string. To correct, replace this

        ab = jobj.optString("text");
        return ab;

with

        JSONArray ar = jobj.optJSONArray("text");
        if (ar != null) {
            return ar.optString(0);
        }
查看更多
登录 后发表回答