一、   JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧。

 Json建构于两种结构:  最后再加一种格式在文章的最后显示出来非常少有的格式

     1、“名称/值”对的集合(A collection of name/value pairs)。不同的语言中。它被理解为对象(object)。纪录(record)。结构(struct),字典(dictionary),哈希表(hash table)。有键列表(keyed list),或者关联数组 (associative array)。 如:     

        {

            “name”:”jackson”,

            “age”:100

         }





    2、值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)如:

     {

        “students”:

        [

            {“name”:”jackson”,“age”:100},

            {“name”:”michael”,”age”:51}

        ]

     }

二、java解析JSON步骤

    A、server端将数据转换成json字符串

      首先、server端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些能够到JSON-lib官网下载:http://json-lib.sourceforge.net/)

 

 JSON <wbr>之JAVA <wbr>解析

    然后将数据转为json字符串,核心函数是:

 public static String createJsonString(String key, Object value)

    {

        JSONObject jsonObject = new JSONObject();

        jsonObject.put(key, value);

        return jsonObject.toString();

    }

B、client将json字符串转换为对应的javaBean

   1、client获取json字符串(由于android项目中已经集成了json的jar包所以这里无需导入)

public class HttpUtil

{

   

    public static String getJsonContent(String urlStr)

    {

        try

        {// 获取HttpURLConnection连接对象

            URL url = new URL(urlStr);

            HttpURLConnection httpConn = (HttpURLConnection) url

                    .openConnection();

            // 设置连接属性

            httpConn.setConnectTimeout(3000);

            httpConn.setDoInput(true);

            httpConn.setRequestMethod("GET");

            // 获取对应码

            int respCode = httpConn.getResponseCode();

            if (respCode == 200)

            {

                return ConvertStream2Json(httpConn.getInputStream());

            }

        }

        catch (MalformedURLException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        catch (IOException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return "";

    }





   

    private static String ConvertStream2Json(InputStream inputStream)

    {

        String jsonStr = "";

        // ByteArrayOutputStream相当于内存输出流

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];

        int len = 0;

        // 将输入流转移到内存输出流中

        try

        {

            while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)

            {

                out.write(buffer, 0, len);

            }

            // 将内存流转换为字符串

            jsonStr = new String(out.toByteArray());

        }

        catch (IOException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return jsonStr;

    }

}

2、获取javaBean

    public static Person getPerson(String jsonStr)

    {

        Person person = new Person();

        try

        {// 将json字符串转换为json对象

            JSONObject jsonObj = new JSONObject(jsonStr);

            // 得到指定json key对象的value对象

            JSONObject personObj = jsonObj.getJSONObject("person");

            // 获取之对象的全部属性

            person.setId(personObj.getInt("id"));

            person.setName(personObj.getString("name"));

            person.setAddress(personObj.getString("address"));

        }

        catch (JSONException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }





        return person;

    }





    public static List<Person> getPersons(String jsonStr)

    {

        List<Person> list = new ArrayList<Person>();





        JSONObject jsonObj;

        try

        {// 将json字符串转换为json对象

            jsonObj = new JSONObject(jsonStr);

            // 得到指定json key对象的value对象

            JSONArray personList = jsonObj.getJSONArray("persons");

            // 遍历jsonArray

            for (int i = 0; i < personList.length(); i++)

            {

                // 获取每个json对象

                JSONObject jsonItem = personList.getJSONObject(i);

                // 获取每个json对象的值

                Person person = new Person();

                person.setId(jsonItem.getInt("id"));

                person.setName(jsonItem.getString("name"));

                person.setAddress(jsonItem.getString("address"));

                list.add(person);

            }

        }

        catch (JSONException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }





        return list;

    }









这里是第三种格式 

{"state":"true","classNo":["HA_2012","4567","4566","test001"]}

解析的方法为

if (result.getString("state").equals("true")) {

try {





JSONArray show=result.getJSONArray("classNo");



List<String> list=new ArrayList<String>();

for(int i=0;i<show.length();i++){

list.add(show.getString(i));

}

到这里三种结束

android JSON数据格式 解析的更多相关文章

  1. Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示

    Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示 今天项目中要实现一个天气的预览,加载的信息很多,字段也很多,所以理清了一下思路,准备独立出来写一个总结,这样对大家 ...

  2. Android JSON数据解析(数据传输)

    上篇随笔详细介绍了三种解析服务器端传过来的xml数据格式,而对于服务器端来说,返回给客户端的数据格式一般分为html.xml和json这三 种格式,那么本篇随笔将讲解一下json这个知识点,包括如何通 ...

  3. Android json 数据解析

    1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...

  4. Android JSON数据解析(GSON方式)

    要创建和解析JSON数据,也可以使用GSON来完成.GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库.使用GSON,可以很容易的将一串JSON数据转换为一个Jav ...

  5. Android Json数据解析

    1.通过主Activity的Button按钮进行解析 public class MainActivity extends Activity { private Button button=null; ...

  6. JSON数据格式解析

    JSON数据的语法规则 1.数据以键值对的形式 2.数据由逗号分隔 3.花括号保存对象 4.方括号保存数组 以PHP的数组为例: <?php $arr = array( "aaaa&q ...

  7. Android JSON语法解析示例

    参考: http://www.open-open.com/lib/view/open1326376799874.html https://www.cnblogs.com/jycboy/p/json_x ...

  8. android 解析json数据格式(转)

    json数据格式解析我自己分为两种: 一种是普通的,一种是带有数组形式的: 普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid" ...

  9. android 解析json数据格式

    json数据格式解析我自己分为两种: 一种是普通的,一种是带有数组形式的: 普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid" ...

随机推荐

  1. light oj 1047-neighbor house

    ime Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description The people ...

  2. ubuntu15.04安装hexo

    首先吐槽一下npm淘宝源,貌似中国目前唯一一个npm源,现在不好用了,不知道是不是换了地址,在吐槽一下万恶的墙!你懂得. 好了,说点正儿八经的事儿. 之所以安装hexo也是为了创建自己的博客,我只说最 ...

  3. Ajax以及类似百度搜索框的demo

    public class Ajax01 extends HttpServlet{ @Override protected void service(HttpServletRequest request ...

  4. windows无效字符名导致的错误及解决办法

    今天用file_put_content($fileName,$data)产生错误:内容如下: Warning: file_put_contents(images/7d5636992a7395f9174 ...

  5. libgdx, mouse 关节

    鼠标与body的交互就靠这个mouse 关节了. 在使用中:主要分成3步: 步1:mouseDown : 这个时期,调用world->QueryAABB.它有一个回调接口,并依据鼠标指针指定一个 ...

  6. jsp截取字符串

    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> < ...

  7. cocos2d-x游戏开发系列教程-坦克大战游戏之子弹和地图碰撞

    上篇文章实现了坦克与地图碰撞的检测, 这篇我们继续完成子弹和地图的碰撞检测. 1.先设计一个子弹类Bullet,如下所示: class Bullet : public CCSprite { publi ...

  8. ViewPageAsImage

    var ViewPageAsImage = function(target, label) {  var setting = {   min_height:   4,   min_width:   4 ...

  9. 在uboot里面加入环境变量使用run来运行

    Author:杨正  Date:2014.11.11   Email:yz2012ww@gmail.com QQ:1209758756 在移植uboot的时候,能够在uboot里面加入定义一些自己的环 ...

  10. 使用linq对字符串1,2,3,4,5,6,7,8,9,10求和

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SumI ...