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);
}
}

json java simple-json的更多相关文章

  1. JSON — Java与JSON数据互转

    转换时Bean所要求的: 被转换的Bean必需是public的. Bean被转换的属性一定要有对应的get方法,且一定要是public的. Bean中不能用引用自身的this的属性,否则运行时出现et ...

  2. Java转json

    一.json介绍 1. 作用:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. 2.json以key-value的格式书写,数 ...

  3. 几种Java的JSON解析库速度对比

    java中哪个JSON库的解析速度是最快的? JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考 了.我们很少会去 ...

  4. Introduction to Structured Data json的2种形式 JAVA解析JSON数据 - JsonArray JsonObject

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

  5. Java中json的构造和解析

    什么是 Json? JSON(JvaScript Object Notation)(官网网站:http://www.json.org/)是 一种轻量级的数据交换格式.  易于人阅读和编写.同时也易于机 ...

  6. Java的Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...

  7. java解析json

    1:下载另外一个Java的小包就可以了: http://www.JSON.org/java/json_simple.zip 里面有源码和文档例题和编程的lib包:编程只需要json_simple.ja ...

  8. PHP、Java输出json格式数据

      PHP 输出json. $result = mysql_query($sql); //查询结果 $users=array(); $i=0; while($row=mysql_fetch_array ...

  9. 使用json-lib进行Java和JSON之间的转换 [转]

    [转] http://www.cnblogs.com/mailingfeng/archive/2012/01/18/2325707.html 1. json-lib是一个java类库,提供将Java对 ...

  10. Java集合 Json集合之间的转换

    1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...

随机推荐

  1. 第 六 课 GO语言常量

    http://www.runoob.com/go/go-constants.html 一 常量 是一个简单值的标识符,在程序运行时,不会被修改的量. 常量中的数据类型只可以是布尔型.数字型(整数型.浮 ...

  2. 【转】js中select的基本操作

    判断select选项中 是否存在Value="paraValue"的Item  // 1.判断select选项中 是否存在Value="paraValue"的I ...

  3. mount/umount文件挂载

    用法: mount [-lhV] mount -a [选项] mount [选项] [--source] <源> | [--target] <目录> mount [选项] &l ...

  4. [hdu1402]A * B Problem Plus(NTT)

    解题关键:快速数论变换NTT模板. 注意$ans$数组的$ans[n]$一定要注意置$0$,或者结果从$n-1$开始遍历,这里很容易出错. 代码1:ACdreamer 的板子. 为什么要reverse ...

  5. Entity Framework Code-First(10):Fluent API

    Fluent API in Code-First: We have seen different DataAnnotations attributes in the previous sections ...

  6. location.assign()、location.href、location.replace(url)的不同

    window.location.assign(url) : 加载 URL 指定的新的 HTML 文档. 就相当于一个链接,跳转到指定的url,当前页面会转为新页面内容,可以点击后退返回上一个页面. w ...

  7. Django 的认证系统

    Django自带的用户认证 auth 模块 from django.contrib import autu django.contrib.auth 中提供了许多方法, 这里主要介绍其中三个: auth ...

  8. idea中,使用facets添加完web后,项目已变为web项目,但web.xml中内容经常变为红色,并报错,如何解决?

    这中错误经常是由于配置facets并添加完web后,没有进一步配置web.xml文件,导致web.xml是使用系统默认的. 如图:需要进一步配置web.xml文件,使用我们src/main/webap ...

  9. Http客户端再封装

    Android系统上推荐的Http客户端从Apache变成[HttpURLConnection],主要理由包括 * 不过因为UrlConnection这组接口时间较早(Java 1.0), 接口的设计 ...

  10. 死磕 java同步系列之开篇

    简介 同步系列,这是彤哥想了好久的名字,本来是准备写锁相关的内容,但是java中的CountDownLatch.Semaphore.CyclicBarrier这些类又不属于锁,它们和锁又有很多共同点, ...