繁体   English   中英

从android项目中的url进行json解析

[英]json parsing from url in android project

如何从json中获取没有[“”]仅文本的文本,在android项目中

这是我来自url {“ code”:200,“ lang”:“ en-ru”,“ text”:[“迟到永远好”]的json

我需要文本“ text”:[“迟到总比没有好”],而没有[“”]仅文本:迟到总比没有好

myclass维护性

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);
        }

    }

}

我的JSONPARSER类别

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;

    }

}

从您的json中获取url {“ code”:200,“ lang”:“ en-ru”,“ text”:[“迟到总比没有好”]}尝试一下...

Yout JSON结构

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

您可以使用以下方法获取输出。

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();
    }

更换

 ab = jobj.optString("text");

 JSONArray txt = jobj.getJSONArray("text");
 ab=txt.getString(0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM