Json解析为逻辑锻炼

时间:2015-11-06 12:58:56

标签: java android json

之前,我应该说,我的英语非常糟糕。对不起。 我解析想要Json输出,在我的android项目中。我的json文件是url。 就像那里

{
  "urun": [
    {
      "ID": "1011245",
      "name": "Jeanne Darc-Elbise-jdje57942xl",
      "name_eng": "Jeanne Darc-Dress-jdje57942xl",
      "catID": "142",
      "tedarikciCode": "jdje57942xl",
      "markaID": "30",
      "data1": "4",
      "resim": "var/30/jdje57942xl/siyah_1_jdje57942xl.jpg",
      "resim2": "var/30/jdje57942xl/siyah_2_jdje57942xl.jpg",
      "resim3": "var/30/jdje57942xl/siyah_3_jdje57942xl.jpg",
      "fiyat": "28",
      "ozellik1detay": "44-50"
    }
] 
}

我的解析器类是

    public class JsonParsers
{
final String TAG = "JsonParsers.java";
static InputStream is  =null;
    static JSONObject jObj=null;
static String ParserJson=null;

    public JsonParsers(String yourJsonStringUrl) {
    }

    public JsonParsers() {
        super();
    }

    public String getJsonUrl(String url) throws IOException {
        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 = "";
            while ((line=reader.readLine())!=null)
            {

                sb.append(line+"\n");
                //Log.e("çıktı:",line);
            }
            is.close();
            ParserJson = sb.toString();


        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try {
            jObj=new JSONObject(ParserJson);

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

        return ParserJson;


    }


}

我的asyncTask类是

 private class AsyncTaskParseJSonIncludes extends AsyncTask<String, String, String> {
    final String TAG = "MainActivity.java";

    JSONArray dataJsonArr = null;
    String ObjectStr;






    protected String doInBackground(String... path)  {
       try{

        try{

            JsonParsers parser = new JsonParsers();

            //Json = parser.getJsonUrl(JsonPath);
            ObjectStr=parser.getJsonUrl(JsonPath);
            JSONObject Json= new JSONObject(ObjectStr);
            dataJsonArr=Json.getJSONArray("urun");

            for(int i=0;i<dataJsonArr.length();i++)
            {
                 JSONObject c = dataJsonArr.getJSONObject(i);
                // Log.e("Deneme", c.getString("name"));
                ID.add(Integer.valueOf(c.getString("ID")));
                name.add(c.getString("name"));
                name_eng.add(c.getString("name_eng"));
                //name_py.add(c.getString("name_py"));
                CatID.add(Integer.valueOf(c.getString("CatID")));
                tedarikciCode.add(c.getString("tedarikciCode"));
                 markaID.add(Integer.valueOf(c.getString("markaID")));
                data1.add(Integer.valueOf(c.getString("data1")));
                resimmmm.add(c.getString("resim"));
                resim2.add(c.getString("resim2"));
                resim3.add(c.getString("resim3"));
                fiyat.add(Integer.valueOf(c.getString("fiyat")));
                ozellik1detay.add(c.getString("ozellik1detay"));
              //  ozellik2detay.add(c.getString("ozellik2detay"));




            }
          for(int i=0;i<name.size();i++) {
                Log.e("Deneme", name.get(i));
            }


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

        return null;
    }
    protected void onPreExecute() {}
/////////////////////////////////////////////////////////


}

它正常工作,但它没有返回json值。 我检查解析器类的数据流,它是固定的。 但它只返回第一个值,其他数据不返回。 我不明白它的逻辑问题。有人帮我修理我的代码我非常好笑。谢谢。

2 个答案:

答案 0 :(得分:0)

name_eng.add().

name_eng定义在哪里?

CatID to catID。

右:

Try{A}catch{}
Try{B}catch{}
Try{C}catch{}

错误:

Try{A;B;C}catch{}

答案 1 :(得分:0)

尝试GsonRequest,解析JSON非常简单,使用它的好例子:USE GsonRequest