java - Android应用程序中的JSON解析

标签 java android json parsing arrays

该 URL 具有以下 View 源。我需要解析并获取为“Folio”提供的图像路径。任何人都可以指导如何获取以下路径的数组。提前致谢

     {"SEO":

  {"Marketing":["http://www.tts.com/media/imagepath/Web.jpg","http://www.tts.com/imagepath/ranks.jpg"],
      "Folio":["http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_a_grade_motor_school.html_a_poftfolio_2013_01_11.png",
                                   "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_abc_tarps.html_a_poftfolio_2013_01_10.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_able_scale.html_a_poftfolio_2013_01_10.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_accent_aluminum_windows.html_a_poftfolio_2013_01_10.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_access_health.html_a_poftfolio_2013_01_10.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_acmeda__zipscreen.html_a_poftfolio_2013_01_11.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_aditya_birla_minerals.html_a_poftfolio_2013_01_10.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_advanced_corporate_badges.html_a_poftfolio_2013_01_10.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_aerotech_fans.html_a_poftfolio_2013_01_10.png",
                                              "http://www.tts.com/media/imagepath/portfolio_images/http_arctictern.t.com.au_folio_portfolio_agriculture_victoria_services_.html_a_poftfolio_2013_01_10.png"]},

 "Services":{"Hosting":["http://www.tts.com/media/imagepath/hostinga.jpg","http://www.tts.com/media/imagepath/hostingb.jpg"],

  "Mobile Apps":["http://www.tts.com/media/imagepath/mobileapp.jpg","http://www.tts.com/media/imagepath/mobilesite.jpg"]

}


}

JSON 解析器类包含

public class JSONParser {

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

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            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.e("Buffer Error", "Error converting result " + e.toString());
        }

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

        // return JSON String
        return jObj;

    }
}

主要 Activity 包含

     public class AndroidJSONParsingActivity extends ListActivity {

    private static String url = "http://www.xyz.com/album_pro/array_to_encode";
    private static final String TAG_FOLIO = "SEO";
     JSONArray folio = null;
     @SuppressLint("NewApi")
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);

            JSONParser jParser = new JSONParser();


            try{

                JSONObject json = jParser.getJSONFromUrl(url);
                Log.v("URL",json.toString());
            JSONObject seo = json.getJSONObject("SEO");
            Log.v("seo",seo.toString());
            JSONArray folio = json.getJSONArray("Folio");

            for(int i=0;i< folio.length();i++)
            {
                Log.v("Test", "test");
            }


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

}
}         

在 logcat 文件中运行应用程序后出现的错误如下

03-04 15:36:50.458: W/System.err(23230): org.json.JSONException: No value for Folio
03-04 15:36:50.458: W/System.err(23230):    at org.json.JSONObject.get(JSONObject.java:354)
03-04 15:36:50.458: W/System.err(23230):    at org.json.JSONObject.getJSONArray(JSONObject.java:544)
03-04 15:36:50.458: W/System.err(23230):    at com.example.testgallery.AndroidJSONParsingActivity.onCreate(AndroidJSONParsingActivity.java:37)
03-04 15:36:50.458: W/System.err(23230):    at android.app.Activity.performCreate(Activity.java:5008)
03-04 15:36:50.458: W/System.err(23230):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-04 15:36:50.458: W/System.err(23230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

最佳答案

首先创建一个ArrayList,例如...

ArrayList<String> urlList = new ArrayList<String>();

从 JSONObject Marketing 获取 Folio JSONArray,例如...

JSONArray folio = marketing.getJSONArray("Folio");

现在将所有项目从 folio 放入 urlList,例如...

for(int i=0;i<folio.length();i++){
  urlList.add(folio.getString(i));
}

现在您可以使用这个 urlList。

关于java - Android应用程序中的JSON解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15198431/

相关文章:

java - 按下 GUI 按钮,从数组中随机选择一个单词并将其放置在 JLabel 上

java - 如何从选项卡布局中删除标高?

java - Android内存管理从 native JNI调用Java类并声明数据(用于图像转换)

java - Retrofit2 HTTP 获取 Json 到 RecyclerView

c# - IEnumerable C# 对象到具有名称的数组的 JSON 对象

java - 模块化分区

java - 使用 LinkedQueue 的替代方案?

java - jackson 在 momentJS 中使用的奇怪时间戳值给出了错误的年份

java - 安卓应用架构

json - 如何在django中返回JSON