zoukankan      html  css  js  c++  java
  • java中string与json互相转化

    在Java中socket数据传输时,数据类型往往比較难选择。可能要考虑带宽、跨语言、版本号的兼容等问题。

    比較常见的做法有两种:一是把对象包装成JSON字符串传输,二是採用java对象的序列化和反序列化。

    随着Google工具protoBuf的开源。protobuf也是个不错的选择。对JSON,Object Serialize,ProtoBuf 做个对照。

    1、string转json

    有三种方法

    第一种:string直接转json

    String json = "{"2":"efg","1":"abc"}";   JSONObject json_test = JSONObject.fromObject(json);   将string的双引號转义就可以。适用于字符串较短的

    另外一种:将string转为list后转为json

    List<String> list = new ArrayList<String>();  list.add("username");  list.add("age");  list.add("sex");  JSONArray array = new JSONArray();  array.add(list);     

                    能够使用list的add函数将须要的字符串拼接就可以,可是这个仅仅能使用jsonarry

    第三种:将string转为map后转为json

     Map<String, String> map = new HashMap<String, String>();
            map.put("1", "abc");
    map.put("2", "efg");
    JSONArray array_test = new JSONArray();
    array_test.add(map);
        JSONObject jsonObject = JSONObject.fromObject(map);

                   这里使用map就能够将字符串转化为JSONArray或者JSONObject都能够。可是这里的键不能使用int型

    1、json转string

    先构造json:JSONObject string_to_json = JSONObject.fromObject("{"data": {"pages": [ {"comment": "just for test"},{"comment": "just for test"}],"total_count": 2 },"errcode": 0}");

    对于JSONObject而言就能够直接使用


        JSONObject json_to_data = string_to_json.getJSONObject("data");就可以

    对于JSONArray而言就能够使用这两种

    第一种:JSONArray json_to_strings = json_to_data.getJSONArray("pages");//先将JSONObject里包括的JSONArray取出
        for (Object object : json_to_strings) {//循环读取就可以
         JSONObject json_to_string = JSONObject.fromObject(object);
          json_to_string.get("pages");
       

    另外一种:JSONArray json_to_strings_test = json_to_data1.getJSONArray("pages");//先将JSONObject里包括的JSONArray取出
        for (int i = 0; i < json_to_strings_test.size(); i++) {//循环读取就可以
          JSONObject json_to_string1 = json_to_strings_test.getJSONObject(i);
       

    上面所有程序的測试如图:


    以下贴出代码:

    //string构筑json
    String json = "{"2":"efg","1":"abc"}";
    JSONObject json_test = JSONObject.fromObject(json);  
        System.out.print("json_test:"+json_test);
        System.out.print(" ");
    //使用list构筑json(list仅仅能使用JSONArray)
    List<String> list = new ArrayList<String>();
            list.add("username");
            list.add("age");
            list.add("sex");
            JSONArray array = new JSONArray();
            array.add(list);
            System.out.print("array:"+array);
        System.out.print(" ");
            //初始化HashMap集合并加入数组(json必须键不能是int类型)
            Map<String, String> map = new HashMap<String, String>();
            map.put("1", "abc");
    map.put("2", "efg");
    JSONArray array_test = new JSONArray();
    array_test.add(map);
            System.out.print("array_test:"+array_test);
        System.out.print(" ");
        JSONObject jsonObject = JSONObject.fromObject(map);  
        System.out.print("jsonObject:"+jsonObject);
        System.out.print(" ");
        //解析json
        JSONObject string_to_json = JSONObject.fromObject("{"data": {"pages": [ {"comment": "just for test1"},{"comment": "just for test2"}],"total_count": 2 },"errcode": 0}");
        JSONObject json_to_data = string_to_json.getJSONObject("data");
        JSONArray json_to_strings = json_to_data.getJSONArray("pages");
        for (Object object : json_to_strings) {
         JSONObject json_to_string = JSONObject.fromObject(object);
         json_to_string.get("pages");
         System.out.print("json_to_string.get("pages"):"+json_to_string.get("comment"));
         System.out.print(" ");
       
        JSONObject json_to_data1 = string_to_json.getJSONObject("data");
        JSONArray json_to_strings_test = json_to_data1.getJSONArray("pages");
        for (int i = 0; i < json_to_strings_test.size(); i++) {
          JSONObject json_to_string1 = json_to_strings_test.getJSONObject(i);
          System.out.print("json_to_string1.get("pages"):"+json_to_string1.get("comment"));
          System.out.print(" ");
       

    有新的好的方法希望可以讨论

  • 相关阅读:
    android:ViewPager实现Tabs滑动切换效果
    android:实现退出确认对话框
    jsp初探
    struts2获取前台数据的三种方式
    struts表单验证
    SingleTon
    读取文件中内容并统计排序
    android:TabHost总结
    java i/o
    tomcat7.0连接池配置
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7152219.html
Copyright © 2011-2022 走看看