Android中Json的使用

12 篇文章 0 订阅

1 Json的简介

Json(Javascript Object notation)作为一种轻量级的数据传输格式,在现代软件开发中广泛使用,采用完全独立于语言的文本格式,具有可读性强、编写容易,更利于机器的解析与生成。

2 Json与Xml的比较

Json与xml分别作为轻量级的数据传输格式,在软件开发中有着很重要的作用,同时两者在不同的编程语言中能够很容易的解析与生成。根据自身的优缺点,在实际项目的开发中,选择合适的数据传输格式尤为重要,比较如下

Json与xml具有极为丰富的解析方式

Json与xml比较,json的数据小

Json与xml比较,xml能够更好的描述对象

Json与xml比较,json的传输效率更高

Json与xml比较,在编写方面,xml具有丰富的工具编写

3 Json在android开发中的使用

Json在android开发中有着广泛的使用,主要用于服务器与客户端的数据传输。在java中提供了JSONArray、JSONObject等对象,能够灵活对Json数据进行解析与生成,下面我将以实例来具体说明在java或android开发中json的使用,实例如下:

package com.example.jsonservice;

 

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

 

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

 

public class JsonService {

       privatestatic JsonService jsonService;

 

       privateJsonService() {

 

       }

 

       publicstatic JsonService getInstance() {

              if(jsonService == null) {

                     jsonService= new JsonService();

              }

              returnjsonService;

       }

 

       publicMap<String, String> analysisJson(String json) {

              Map<String,String> items = new HashMap<String, String>();

              try{

                     JSONArrayjsonArray = new JSONArray(json);

                     for(int index = 0; index < jsonArray.length(); index++) {

                            JSONObjectjsonObject = jsonArray.getJSONObject(index);

                            Iterator<String>iterator = jsonObject.keys();

                            while(iterator.hasNext()) {

                                   Stringkey = iterator.next();

                                   Stringvalue = jsonObject.get(key).toString();

                                   items.put(key,value);

                            }

                     }

              }catch (JSONException e) {

                     e.printStackTrace();

              }

              returnitems;

       }

 

       publicString generateJson(Map<String, String> items) {

              Stringjson = "";

              try{

                     Iterator<String>iterator = items.keySet().iterator();

                     JSONObjectjsonObject = new JSONObject();

                     while(iterator.hasNext()) {

                            Stringkey = iterator.next().toString();

                            String value = items.get(key);

                            jsonObject.put(key,value);

                     }

                     json= jsonObject.toString();

              }catch (JSONException e) {

                     e.printStackTrace();

              }

              returnjson;

       }

}

 

在以上代码段中,支持单层次结构的Json数据解析,后期希望能够根据实际应用,能够编写出通用的json数据解析方法,如果大家有更好的建议或意见谢谢反馈。为此我从你们的反馈中学习然后总结,我将不甚感激。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值