Json

  JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等).

  Json简单说就是javascript中的对象和数组,通过这两种结构可以表示各种复杂的结构。

     1.对象:对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值。

   eg.{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }

  2.数组:数组在js中是中括号“[]”括起来的内容,数据结构为 ["java","javascript","vb",...],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。

      使用 JSON,只需将多个带花括号的记录分组在一起:  

      eg.{"people": [

                { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
                { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
                { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
            ]
    }
           
{ "programmers": [
  { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
  { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
  { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
  ],
  "authors": [
  { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
  { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
  { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
    ],
  "musicians": [
  { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
  { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
    ] }
 
Java 解析Json
      加入json-lib.jar包
  
protected String getJsonString(String urlPath) throws Exception {  
        URL url = new URL(urlPath);  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        connection.connect();  
        InputStream inputStream = connection.getInputStream();  
        //对应的字符编码转换  
        Reader reader = new InputStreamReader(inputStream, "UTF-8");  
        BufferedReader bufferedReader = new BufferedReader(reader);  
        String str = null;  
        StringBuffer sb = new StringBuffer();  
        while ((str = bufferedReader.readLine()) != null) {  
            sb.append(str);  
        }  
        reader.close();  
        connection.disconnect();  
        return sb.toString();  
    }  

  

        JSONObject jsonObject = new JSONObject(result); 
        JSONArray subjects=jsonObject.getJSONArray("subjects");
        JSONObject subject;
        JSONObject imgs;
        for(int i=0;i<subjects.length();i++)
        {
            movie=new Movie();
            subject=subjects.getJSONObject(i);
            movie.setMovie_id(subject.getString("id"));
            movie.setTitle(subject.getString("title"));
            movie.setYear(subject.getString("year"));
            //类别标签
            movie.setTag(tag);
            //图片url
            imgs=subject.getJSONObject("images");
            movie.setImgUrl(imgs.getString("large"));
            //加入列表
            movieList.add(movie);
        }

 

posted @ 2013-12-20 21:01  DET橙  阅读(184)  评论(0编辑  收藏  举报