• json java simple-json


    http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-1_-_Encode_a_JSON_object

    javac -cp ./json-simple.jar SplitDemo.java

    java -cp _CLASSPATH:./json-simple.jar SplitDemo

    public class QueryAttribute implements JSONStreamAware {
    
       private String query;
       private String cat;
       private int guanwang;
       private String icon;
       private String entity_name;
       private double score;
       private String clkurl;
    
       public QueryAttribute(String query, double score, String cat,
            String entity_name, int guanwang, String icon, String clkurl) {
         this.query = query;
         this.cat = cat;
         this.entity_name = entity_name;
         this.score = score;
         this.guanwang = guanwang;
         this.icon = icon;
         this.clkurl = clkurl;
       }
    
       public void writeJSONString(Writer out) throws IOException {
         LinkedHashMap obj = new LinkedHashMap();
         obj.put("query", query);
         obj.put("score", score);
         obj.put("cat", cat);
         obj.put("ename", entity_name);
         obj.put("gw", guanwang);
         obj.put("icon", icon);
         obj.put("clkurl", clkurl);
         JSONValue.writeJSONString(obj, out);
       }
    }

    parser

    import org.json.simple.JSONObject;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONValue;
    import java.util.*;
    public class Segment
    {
        public static void main(String[] args)
        {
            String s="[0,{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}]";
            Object obj = JSONValue.parse(s);
            JSONArray array = (JSONArray)obj;
            System.out.println(array.get(0));
            System.out.println(array.get(1));
            System.out.println();
            JSONObject obj2 = (JSONObject)array.get(1);
            System.out.println("=============field "1"=======");
            System.out.println(obj2.get("1"));
            System.out.println(obj);
    
            s = "{}";
            obj = JSONValue.parse(s);
            System.out.println(obj);
    
            s = "[5,]";
            obj = JSONValue.parse(s);
            System.out.println(obj);
    
            s = "[5,,2]";
            obj = JSONValue.parse(s);
            System.out.println(obj);
        }
    }
  • 相关阅读:
    232. Implement Queue using Stacks
    231. Power of Two
    n&(n-1)位运算的妙用
    230. Kth Smallest Element in a BST
    关于UNIX的exec函数
    Go语言的各种Print函数
    Go语言的接口interface、struct和组合、继承
    Go语言知识点笔记
    Ubuntu自定义终端窗口位置
    Python的类变量和成员变量、类静态方法和类成员方法
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/3889152.html
Copyright © 2020-2023  润新知