简单的JsonHelper

Java JSON 数据构造
本文介绍了一个简单的 Java 实现,用于构建 JSON 数据结构。通过循环添加数据项并转换为字符串形式,展示了如何创建 JSON 数组及元素。代码示例中包含了设置成功标志、错误消息以及数据项的方法。

 

None.gifpackage utils;
None.gif
None.gif
import java.util.ArrayList;
None.gif
None.gif
public class Json
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
public static void main(String[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Json json 
= new Json();
InBlock.gif        json.reSet();
InBlock.gif        json.setSuccess(
true);
InBlock.gif
InBlock.gif        
for (int n = 0; n < 5; n++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            json.addItem(
"a", n + "a");
InBlock.gif            json.addItem(
"b", n + "b");
InBlock.gif            json.addItem(
"c", n + "c");
InBlock.gif            json.addItem(
"d", n + "c");
InBlock.gif            json.addItem(
"e", n + "e");
InBlock.gif            json.addItem(
"f", n + "f");
InBlock.gif            json.addItemOk();
ExpandedSubBlockEnd.gif        }

InBlock.gif        System.out.println(json.ToString());
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public String singleInfo = "";
InBlock.gif    
protected boolean _success = true;
InBlock.gif    
protected String _error = "";
InBlock.gif    
protected ArrayList arrData = new ArrayList();
InBlock.gif    
protected ArrayList dataItem = new ArrayList();
InBlock.gif
InBlock.gif    
public String getError()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return _error;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void setError(String error)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (!error.equals(""))
InBlock.gif            
this._success = false;
InBlock.gif        
this._error = error;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public boolean getSuccess()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return _success;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void setSuccess(boolean success)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (success)
InBlock.gif            
this._error = "";
InBlock.gif        
this._success = success;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public Json()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void reSet()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        arrData.clear();
InBlock.gif        dataItem.clear();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void addItem(String name, String _value)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        dataItem.add(name);
InBlock.gif        dataItem.add(_value);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 一个数组添加完毕,一个新的数组开始
InBlock.gif
    public void addItemOk()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        arrData.add(dataItem);
InBlock.gif        dataItem 
= new ArrayList();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public String ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        StringBuilder sb 
= new StringBuilder();
InBlock.gif        
// sb.append("{");
InBlock.gif        
// sb.append("\"datas\":");
InBlock.gif
        sb.append("[");
InBlock.gif        
int ad = arrData.size();
InBlock.gif        
for (int i = 0; i < ad; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList arr 
= (ArrayList) (arrData.get(i));
InBlock.gif            sb.append(
"{");
InBlock.gif            
int t = arr.size();
InBlock.gif            
for (int j = 0; j < t; j += 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (j == t)
InBlock.gif                    
break;
InBlock.gif                sb.append(
"\"");
InBlock.gif
                sb.append(arr.get(j).toString());
InBlock.gif                sb.append(
"\"");
InBlock.gif
                sb.append(":");
InBlock.gif                sb.append(
"\"");
InBlock.gif
                sb.append(arr.get(j + 1).toString());
InBlock.gif                sb.append(
"\"");
InBlock.gif
                if (j < t - 2)
InBlock.gif                    sb.append(
",");
ExpandedSubBlockEnd.gif            }

InBlock.gif            sb.append(
"}");
InBlock.gif            
if (i < ad - 1)
InBlock.gif                sb.append(
",");
ExpandedSubBlockEnd.gif        }

InBlock.gif        sb.append(
"]");
InBlock.gif        
// sb.append("}");
InBlock.gif
        return sb.toString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值