简体   繁体   中英

Multiple JSON objects and get array in android

I have problem in parsing JSON. http://www.json-generator.com/api/json/get/cdWQqIXQfC?indent=2 Actually i want to get the below

"jean": {
          "color": "red", 
          "cost": 15000, 
          "size": 28
        }

to get from URL . then need to set listview. But first i didnt get json array itself.\\ The below code throws exception and shows no value for men.

I tried using

public class MainActivity extends Activity {
    String firstname = null;
    String lastname = null;
    String username = null;
    String username1 = null;

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

    public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
        final String TAG = "AsyncTaskParseJson.java";
        String yourJsonStringUrl = "http://www.json-generator.com/api/json/get/cdWQqIXQfC?indent=2";
        JSONArray dataJsonArr = null;
        JSONObject dataJsonArry = null;
        JSONObject dataJsonArry1 = null;
        JSONObject dataJsonArry2 = null;

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected String doInBackground(String... arg0) {
            try {
                JsonParser jParser = new JsonParser();
                JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
                dataJsonArry = json.getJSONObject("online");
                dataJsonArry1 = json.getJSONObject("men");
                dataJsonArry2 = json.getJSONObject("clothes");
                dataJsonArr = json.getJSONArray("jean");
                for (int i = 0; i < dataJsonArr.length(); i++) {
                    JSONObject c = dataJsonArr.getJSONObject(i);
                    lastname = c.getString("cost");
                    username = c.getString("size");
                    Log.e("Hello", "firstname: " + firstname + ", lastname: "
                            + lastname + ", username: " + username + "star:"
                            + username1);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String strFromDoInBg) {
        }
    }
}

json parser:

public class JsonParser {

    final String TAG = "JsonParser.java";

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    public JSONObject getJSONFromUrl(String url) {

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            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.d(TAG, "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e(TAG, "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;
    }
}

I dont know how to acheive this ... Can anyone help me ...

Jean is a JSONObject and you use JSONArray in your code. Moreover size and cost are numbers but you use getString() . So this code should work (please use relevant name for your variables : do not store a cost in a variable called lastname)

JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
JSONObject dataJsonOnline = json.getJSONObject("online");
JSONObject dataJsonMen = dataJsonOnline.getJSONObject("men");
JSONObject dataJsonCloth = dataJsonMen.getJSONObject("clothes");
JSONObject dataJsonJean = dataJsonCloth.getJSONObject("jean");

String color = dataJsonJean.getString("color");
int cost  = dataJsonJean.getInt("cost");
int size = dataJsonJean.getString("size");

try this:

try { 
            JsonParser jParser = new JsonParser();
            JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
            JSONObject onlineJsonObj = json.getJSONObject("online");
            JSONObject menJsonObj = online.getJSONObject("men");
            JSONObject clothesJsonObj= menJsonObj.getJSONObject("clothes");
            JSONObject jeanJsonObj= clothesJsonObj.getJSONObject("jean");

                lastname = clothesJsonObj.getString("cost");
                username = clothesJsonObj.getString("size");
                Log.e("Hello", "firstname: " + firstname + ", lastname: "
                        + lastname + ", username: " + username + "star:"
                        + username1);
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        } 
        return null; 
    } 

There is no JSONArray in you respose. JsonArray is between '['and ']' brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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