0

我在使用以下代码解析 JSON 对象和数组时遇到问题。请参阅以下 JSON 数据,我试图从中获取文本并将其插入 TextViews。由于某种原因,目前我收到一个summary无法找到的错误。有关详细信息,请参阅 JSONParser 类。

尝试解析以下 URL 是针对摘要数据。

https://api.company.com/api/systems/165756/summary?&key=e1e63de7276b04c9bb99adfd45b3a14c

在 JSON 验证器 ( http://jsonlint.com/ ) 中检查 url 我得到以下信息

 {
  "energy_month": 31132,
  "current_power": 4210,
  "modules": 24,
  "energy_today": 14666,
  "system_id": 165756,
  "energy_week": 215504,
  "source": "microinverters",
  "energy_lifetime": 1545467,
  "summary_date": "2013-05-02T00:00:00-07:00"
 }

这是我用来尝试解析其中一些字符串(energy_month)(current_power)等的代码。在我的JSONParser.java中,我必须分类解析数组或对象。这可能需要修复。

 protected ArrayList<String> doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();
             arrfortextviews=new ArrayList<String>();
            // Using APIKEY from strings.xml
            String apikey = getString(R.string.apikey);

            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            String systemID = settings.getString("systemID", "");

            //JSONArray json = jParser.getJSONFromUrl(url2 + systemID + "/summary?&key=" + apikey);
            JSONObject json2 = jParser.getJSONFromUrl2(url2 + systemID + "/summary?&key=" + apikey);
            //for (int i = 0; i < json2.length(); i++) {
                //i = index
                try {
                    //JSONObject c = json2.getJSONObject(i);
                    //JSONObject geometry = json2.getJSONObject("summary");
                    Log.e("JSON Parser", json2 + args.toString());

                    String current_power = json2.getString(TAG_CURRENT_POWER);
                    String energy_lifetime = json2.getString(TAG_ENERGY_LIFETIME);

                    arrfortextviews.add(current_power);
                    arrfortextviews.add(energy_lifetime);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("JSON Parser", json2 + e.toString());
                }
            return arrfortextviews;

        }

JSONParser.class

   public class JSONParser {

private static final Context context = null;
static InputStream is = null;
static JSONObject jarray = null;
static JSONArray jarray2 = null;
static String json = "";

// constructor
public JSONParser() {

}
public JSONObject getJSONFromUrl2(String url) {

       StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
              builder.append(line);
            }
          } else {
            Log.e("==>", "No Response, Check Your API KEY");
            Toast.makeText(context,"Error Response, Check your API Key", Toast.LENGTH_LONG).show();
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
    // try parse the string to a JSON object
    try {
        JSONObject jobj = new JSONObject(builder.toString());
        jarray = jobj.getJSONObject("summary");
        Log.e("JSON Parser", jobj + url.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
        Log.e("JSON Parser", json + url + e.toString());
    }

    // return JSON String
    return jarray;

}
public JSONArray getJSONFromUrl(String url) {

       StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
              builder.append(line);
            }
          } else {
            Log.e("==>", "No Response, Check Your API KEY");
            Toast.makeText(context,"Error Response, Check your API Key", Toast.LENGTH_LONG).show();
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

    // try parse the string to a JSON object
    try {
        JSONObject jobj = new JSONObject(builder.toString());
        jarray2 = jobj.getJSONArray("systems");
        Log.e("JSON Parser", jarray2 + json + url.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
        Log.e("JSON Parser", json + url + e.toString());
    }
    // return JSON String
    return jarray2;
   }
4

0 回答 0