一、   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. net core开发环境准备

    net core开发环境准备 1.1  安装sdk和运行时 浏览器打开网址https://www.microsoft.com/net/download, 到.Net Core下载页面. 根据操作系统, ...

  2. redis(四)redis与Mybatis的无缝整合让MyBatis透明的管理缓存

    redis的安装 http://liuyieyer.iteye.com/blog/2078093 redis的主从高可用  http://liuyieyer.iteye.com/blog/207809 ...

  3. grep egrep fgrep命令

    一.grep.egrep.fgrep命令 本文中主要介绍了linux系统下grep egrep fgrep命令和正则表达式的基本参数和使用格式.方法.(注释:文中fg代表例子,) 1.1.基本定义: ...

  4. java的new BufferedReader(new InputStreamReader(System.in))

    流 JAVA /IO 基本小结 通过一行常见的代码讨论:new BufferedReader(new InputStreamReader(System.in)) /*** *** 看到这篇文章挺好的, ...

  5. 自己设计的SSO登录流程图

    这个图上不考虑安全加密.由于本身SSO流程图已经比較复杂了.可能还有问题,欢迎大家拍砖. 1.登录流程图: 2.退出流程图: 3.改进方面: 每一个应用登录后.直接将ticket写入session中, ...

  6. iOS:(接口适配器3)--iPhone适应不同型号 6/6plus 前

    对于不同的苹果设备.检查每个参数<iOS:机型參数.sdk.xcode各版本号>.        机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了.表示机器屏幕尺寸变大了: 像素:表示屏幕 ...

  7. UVALive 3989 Ladies&#39; Choice

    经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format:  ...

  8. 简单字符串处理 hdu1062 Text Reverse

    虽然这个题目一遍AC,但是心里还是忍不住骂了句shit! 花了一个小时,这个题目已经水到一定程度了,但是我却在反转这个操作上含糊不清,并且还是在采用了辅助数组的情况下,关系的理顺都如此之难. 其实我是 ...

  9. sqlplus

    以超级管理员登录 sqlplus sys/123 as sysdba 解锁用户 alter user xutianhao account unlock

  10. Java正则表达式基本应用

    一.概述 正则表达式因为其强大的字符串处理能力,刚开始被被广泛地应用到各种UNIX工具中,如大家熟知的 Perl脚本语言 .后来正则表达式在各种 计算机语言 和各种应用领域得到了广泛的应用和发展,目前 ...