Java Android:Sort JSON

Java Android:Sort JSON,java,android,android-layout,textview,Java,Android,Android Layout,Textview,不熟悉使用RESTAPI和JSON文件,但我已经从天气API检索到了有关我当前位置和天气状况的数据。JSON文件包含数据,例如我的位置、天气速度等。我希望将这些数据的各个部分排序到文本视图中,以便可以清楚地看到它们 我的异步类: import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import org.json.JSONArray; impo

不熟悉使用RESTAPI和JSON文件,但我已经从天气API检索到了有关我当前位置和天气状况的数据。JSON文件包含数据,例如我的位置、天气速度等。我希望将这些数据的各个部分排序到文本视图中,以便可以清楚地看到它们

我的异步类:

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import java.util.ArrayList;
import android.widget.*;
import java.util.Date;
import android.util.Log;

public class RESTAPI extends Activity {

ArrayList<String> items = new ArrayList<String>();
// json test string
String jsonTest;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_restapi);
    // start the  AsyncTask for calling the REST service using httpConnect class
    new AsyncTaskParseJson().execute();
}


// added asynctask class methods below -  you can make this class as a separate class file
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

    // set the url of the web service to call
    String yourServiceUrl = "http://api.apixu.com/v1/current.json?key=e87e62510df946cc84c02652162112&q=LN11RX";

    @Override

    protected void onPreExecute() {
    }

    @Override

    protected String doInBackground(String... arg0) {

        try {
            // create new instance of the httpConnect class
            httpConnect jParser = new httpConnect();

            // get json string from service url
            String json = jParser.getJSONFromUrl(yourServiceUrl);

            // save returned json to your test string
            jsonTest = json.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String strFromDoInBg) {
        TextView tv1 = (TextView) findViewById(R.id.jsontext);
        tv1.setText(jsonTest);
    }

}
那么,有没有办法对返回的数据进行排序,并将每一位数据放入自己的文本框中呢

JSON的屏幕截图:


如果我正确理解了你的问题,试着试一试。如果我误解了,请告诉我,我会尝试帮助您找到替代方案

编辑: 动态创建新文本视图和设置数据的大致另一种方法:

TextView view;
LinearLayout currLayout = (LinearLayout) findViewById(R.id.LinearLayout);
for(String value : items) {
    view = new TextView();
    view.setText(value);
    currLayout.addView(view);
}

如果我对你的问题理解正确,试着试一试。如果我误解了,请告诉我,我会尝试帮助您找到替代方案

编辑: 动态创建新文本视图和设置数据的大致另一种方法:

TextView view;
LinearLayout currLayout = (LinearLayout) findViewById(R.id.LinearLayout);
for(String value : items) {
    view = new TextView();
    view.setText(value);
    currLayout.addView(view);
}
  • 为得到的响应创建pojo类:
按如下方式打开任何转换站点:-

  • 在这里粘贴json响应,然后单击zip,所有pojo类将自动为您创建
现在在您的代码中执行此操作

protected String doInBackground(String... arg0) {

        try {
            // create new instance of the httpConnect class
            httpConnect jParser = new httpConnect();

            // get json string from service url
            String json = jParser.getJSONFromUrl(yourServiceUrl);

            // save returned json to your test string
            jsonTest = json.toString();
Gson gson = new Gson();
/*here Example class is the main pojo class, you can use this class which will be there in the zip, which is created from jsontoPojo converting site */

Example  response = gson.fromJson(json, Example.class);

/*
 *Now to get data 
 * just do this */

String name = getLocation().getName();
.
.
.


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
有关此示例的详细信息

您还需要在build.gradle文件中添加此依赖项

compile 'com.google.code.gson:gson:2.4'
  • 为得到的响应创建pojo类:
按如下方式打开任何转换站点:-

  • 在这里粘贴json响应,然后单击zip,所有pojo类将自动为您创建
现在在您的代码中执行此操作

protected String doInBackground(String... arg0) {

        try {
            // create new instance of the httpConnect class
            httpConnect jParser = new httpConnect();

            // get json string from service url
            String json = jParser.getJSONFromUrl(yourServiceUrl);

            // save returned json to your test string
            jsonTest = json.toString();
Gson gson = new Gson();
/*here Example class is the main pojo class, you can use this class which will be there in the zip, which is created from jsontoPojo converting site */

Example  response = gson.fromJson(json, Example.class);

/*
 *Now to get data 
 * just do this */

String name = getLocation().getName();
.
.
.


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
有关此示例的详细信息

您还需要在build.gradle文件中添加此依赖项

compile 'com.google.code.gson:gson:2.4'

我已经有了Json数组来保存Json对象,它被称为Json或字符串JsonTest,我想循环搜索这些对象,如location或name,然后将其拆分为TextView。我认为最好的方法是将其映射到Java对象,如
location
,其中包括Json字符串中的所有变量/字段。这样,您就可以更轻松地访问getter/setter方法,然后只需调用
Location.getName()
Location.getRegion()
等。如果您最终决定使用此方法,那么您可以使用Gson(我提供的链接)将JSON映射到Location对象。例如:
Location loc=gson.fromJson(jsonTest,Location.class)所以我已经有了保存Json对象的Json数组,它被称为Json或字符串JsonTest,我想循环搜索这些对象,如location或name,然后将其拆分为TextView。我认为最好的方法是将其映射到Java对象,如
location
,其中包括Json字符串中的所有变量/字段。这样,您就可以更轻松地访问getter/setter方法,然后只需调用
Location.getName()
Location.getRegion()
等。如果您最终决定使用此方法,那么您可以使用Gson(我提供的链接)将JSON映射到Location对象。例如:
Location loc=gson.fromJson(jsonTest,Location.class)