如何在android中解析没有数组名称的json结果

时间:2016-04-23 02:46:09

标签: android arrays json

我试图解析没有数组名称的json结果。我尝试了不同的方法但不适用它们是我的代码。 所以请有人告诉我如何在不使用数组名称的情况下解析json结果

提前感谢。

我的JSON

[{"id":"710","name":"app","address":"k","gender":"h"}]

我正在尝试解析此JSON

{"employees":[{"id":"710","name":"app","address":"k","gender":"h"}]}

我希望它像这样

private class CheckList extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog mProgressDialog;

    @Override
    protected void onPreExecute() {

        mProgressDialog = new ProgressDialog(Main2Activity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Please Wait");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();

        super.onPreExecute();

        Main2Activity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {


            }
        });


    }

    @Override
    protected JSONObject doInBackground(String... args) {
        final JParserAdv jParser = new JParserAdv();
        JSONObject json = jParser.makeHttpRequest("http://afkdemo.glabsapps.com/insertwebservice.asmx/getData?id=710", "GET", null);
        return json;

    }

    @Override
    protected void onPostExecute(JSONObject json) {
        ArrayList<ITEM> data=new ArrayList<ITEM>();
        try {
            //need to insert array name here but json result I'm using is without array name.
            JSONArray arr = json.getJSONArray("arrayname");
            for(int i=0;i<arr.length();i++) {
                ITEM d = new ITEM();
                d.item_rank = ((JSONObject) arr.get(i)).get("id").toString();
                d.item_name = ((JSONObject) arr.get(i)).get("name").toString();
                d.item_population = ((JSONObject) arr.get(i)).get("address").toString();
                data.add(d);
            }
            My_adapter adapter = new My_adapter(Main2Activity.this, data);
            listView.setAdapter(adapter);
            mProgressDialog.dismiss();
        }catch (Exception ex){ex.printStackTrace();}

    }

}

3 个答案:

答案 0 :(得分:0)

看起来您请求的端点返回的JSON结构如下所示:

[
  {
    id: "710",
    name: "app",
    address: "k",
    gender: "h"
  }
]

由于您的根没有标识符,请尝试在doInBackground()方法中将数组作为根返回。

private class CheckList extends AsyncTask<String, String, JSONArray> {

    ...

    @Override
    protected JSONArray doInBackground(String... args) {
        final JParserAdv jParser = new JParserAdv();
        JSONArray json = jParser.makeHttpRequest("http://afkdemo.glabsapps.com/insertwebservice.asmx/getData?id=710", "GET", null);
        return json;
    }

    @Override
    protected void onPostExecute(JSONArray json) {
        ...
    }
}

我不确定JParseAdv API是什么样的,但希望有办法让它返回一个数组作为JSON结构的根目录。

答案 1 :(得分:0)

首先让Employees成为一个数组。

然后获取index = 0处的对象。那应该是诀窍!

E.g:

JSONArray employees = json.optJSONArray("employees");
JSONObject anEmployee = employees.optJSONObject(0);

答案 2 :(得分:0)

我有同样的问题。以下代码对我有用:

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private RequestQueue queue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = findViewById(R.id.text_view_result);
    Button buttonParse = findViewById(R.id.button_parse);

    queue = Volley.newRequestQueue(this);

    buttonParse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            jsonParse();
        }
    });
}


private void jsonParse() {

    String url = "http://10.216.70.19:8080/restServices/webapi/services/getAGVStatusList"; //This is my Json url
    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //weatherData.setText("Response is :- ");
            parseData(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            textView.setText("Data Not Received");

        }
    });

    queue.add(request);
    super.onStart();
}

private void parseData(String response) {
    try {
        // Create JSOn Object
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i <jsonArray.length() ; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            textView.setText(jsonObject.getString("batteryLevel")); //get the desired information from the Json object

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}