多个JSON对象并在android中获取数组

时间:2015-11-04 07:57:46

标签: java android arrays json

我在解析JSON时遇到问题。 http://www.json-generator.com/api/json/get/cdWQqIXQfC?indent=2 其实我想得到以下

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

从URL获取。然后需要设置listview。但首先我没有得到json数组本身。 以下代码抛出异常并且对男性没有任何价值。

我尝试使用

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

json解析器:

dependent: :destroy

我不知道如何实现这一点......任何人都可以帮助我......

2 个答案:

答案 0 :(得分:0)

Jean是JSONObject,您在代码中使用JSONArray。此外,sizecost是数字,但您使用getString()。所以这段代码应该可行(请使用变量的相关名称:不要在名为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");

答案 1 :(得分:0)

试试这个:

 void setup() {
  Serial.begin(9600);
  Serial.println("Start");
}

void loop() {
  if(Serial.available()){
   int a = Serial.read();
   Serial.print(a);
 }
}

你没有JSONArray。 JsonArray位于'['和']'括号之间。