zoukankan      html  css  js  c++  java
  • 远程服务器存储之JSON

    一.特点

        1.JavaScript Object Notation

        2.一种轻量级的数据交互格式

    二.格式

        1.[ ] 数组:[value1, value2, value3...]

        2.{ } 对象:{key1:value1, key2:value2, key3:value3,...}

          1-key:字符串,表示对象的属性

          2-value:表示属性的值

             数据类型:数值,字符串,null,json数组,json对象。

    三.API

       1.Android原生

       2.第三方框架

     JSON代码展示:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp3.TestActivity4"
    11     android:orientation="vertical">
    12 
    13     <Button
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:text="Android原生解析JSON转对象"
    17         android:onClick="bt1_OnClick"/>
    18 
    19     <Button
    20         android:layout_width="match_parent"
    21         android:layout_height="wrap_content"
    22         android:text="Gson解析JSON转对象"
    23         android:onClick="bt2_OnClick"/>
    24 
    25     <Button
    26         android:layout_width="match_parent"
    27         android:layout_height="wrap_content"
    28         android:text="Android原生解析JSON转集合"
    29         android:onClick="bt3_OnClick"/>
    30 
    31     <Button
    32         android:layout_width="match_parent"
    33         android:layout_height="wrap_content"
    34         android:text="Gson解析JSON转集合"
    35         android:onClick="bt4_OnClick"/>
    36 
    37     <Button
    38         android:layout_width="match_parent"
    39         android:layout_height="wrap_content"
    40         android:text="Gson解析JSON之对象转JSON"
    41         android:onClick="bt5_OnClick"/>
    42 
    43     <Button
    44         android:layout_width="match_parent"
    45         android:layout_height="wrap_content"
    46         android:text="Gson解析JSON之集合转JSON"
    47         android:onClick="bt6_OnClick"/>
    48 
    49 
    50 
    51 
    52     <EditText
    53         android:layout_width="match_parent"
    54         android:layout_height="200dp"
    55         android:id="@+id/et_3"/>
    56 
    57 
    58 </LinearLayout>
    .xml
      1 package com.hanqi.testapp3;
      2 
      3 import android.support.v7.app.AppCompatActivity;
      4 import android.os.Bundle;
      5 import android.view.View;
      6 import android.widget.EditText;
      7 
      8 import com.google.gson.Gson;
      9 import com.google.gson.reflect.TypeToken;
     10 
     11 import org.json.JSONArray;
     12 import org.json.JSONException;
     13 import org.json.JSONObject;
     14 
     15 import java.util.ArrayList;
     16 import java.util.List;
     17 import java.util.Map;
     18 
     19 public class TestActivity4 extends AppCompatActivity {
     20 
     21     EditText et_3;
     22 
     23     @Override
     24     protected void onCreate(Bundle savedInstanceState) {
     25         super.onCreate(savedInstanceState);
     26         setContentView(R.layout.activity_test4);
     27 
     28         et_3=(EditText)findViewById(R.id.et_3);
     29 
     30     }
     31 
     32     //原生从Json字符串转成对象
     33     public void bt1_OnClick(View v)
     34     {
     35 
     36         String strJson="{"id":1,"name":"大虾", " +
     37                 ""price":12.3, " +
     38                 ""imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"}";
     39 
     40         //从Json字符串转成对象
     41         try {
     42             JSONObject jo = new JSONObject(strJson);
     43 
     44             int id=jo.getInt("id");
     45             String name=jo.getString("name");
     46             double price=jo.getDouble("price");
     47             String imagePath=jo.getString("imagePath");
     48 
     49             ShopInfo shopInfo=new ShopInfo(id,imagePath,name,price);
     50 
     51             et_3.setText(shopInfo.toString());
     52 
     53 
     54         }
     55         catch (Exception e)
     56         {
     57             e.printStackTrace();
     58         }
     59 
     60 
     61     }
     62 
     63     //Gson转对象
     64     public void bt2_OnClick(View v)
     65     {
     66         //转对象
     67         String jsonstr="{"id":3,"name":"大虾"," +
     68                 ""price":12.3," +
     69                 ""imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"}";
     70 
     71         ShopInfo shopInfo=new Gson().fromJson(jsonstr,ShopInfo.class);
     72 
     73         et_3.setText(shopInfo.toString());
     74 
     75 //        //转集合
     76 //        String jsonstr1="[{"id":3, "name":"大虾1", "price":12.3, " +
     77 //                ""imagePath":"http://192.168.10.165:8080/f1.jpg"}," +
     78 //                "{"id":4, "name":"大虾2", "price":12.5, " +
     79 //                ""imagePath":"http://192.168.10.165:8080/f2.jpg"}]";
     80 //
     81 //        List<ShopInfo> list=new Gson().fromJson(jsonstr1, new TypeToken<List<ShopInfo>>() {
     82 //        }.getType());
     83 //
     84 //        et_3.setText(list.toString());
     85 //
     86 //        //对象转JSON
     87 //        ShopInfo info=new ShopInfo(3,"http://www.sina.com","KK",1000);
     88 //
     89 //        String json=new Gson().toJson(info);
     90 //
     91 //        et_3.setText(json);
     92 //
     93 //        //集合转JSON
     94 //        List<ShopInfo> list1=new ArrayList<ShopInfo>();
     95 //
     96 //        list1.add(new ShopInfo(3, "http://www.sina.com","KK", 1000));
     97 //        list1.add(new ShopInfo(4, "http://www.sina.com/cn", "KK1", 2000));
     98 //        String json1=new Gson().toJson(list1);
     99 //
    100 //        et_3.setText(json1);
    101 
    102 //        String jsonstr2="{"my name":"大虾","1":12}";
    103 //
    104 //        Map<String,Object> map=new Gson().fromJson(jsonstr2,new TypeToken<Map<String,Object>>(){}.getType());
    105 //
    106 //        et_3.setText(map.toString());
    107 
    108     }
    109 
    110     //原生从Json字符串转成集合
    111     public void bt3_OnClick(View v)
    112     {
    113 
    114         //转集合
    115         String jsonstr="[{"id":1,"name":"大虾1","price":12.3," +
    116                 " "imagePath":"http://192.168.10.165:8080/f1.jpg"}," +
    117                 "{"id":2, "name":"大虾2", "price":12.5, " +
    118                 ""imagePath":"http://192.168.10.165:8080/f2.jpg"}]";
    119 
    120         //从Json字符串转成集合
    121         try {
    122 
    123             List<ShopInfo> list=new ArrayList<ShopInfo>();
    124 
    125             //1.将json字符串包装JSONArray对象
    126             JSONArray jsonArray=new JSONArray(jsonstr);
    127 
    128             //2.遍历JSONArray对象所有元素(JSONObject),并将每个元素封装为shopInfo,并添加到list
    129             for (int i=0;i<jsonArray.length();i++)
    130             {
    131                 JSONObject jsonObject=jsonArray.getJSONObject(i);
    132 
    133                 //从对象中根据key得到相应的value
    134                 int id=jsonObject.getInt("id");
    135                 String name=jsonObject.getString("name");
    136                 double price=jsonObject.getDouble("price");
    137                 String imagePath=jsonObject.getString("imagePath");
    138 
    139                 //封装ShopInfo对象
    140                 ShopInfo shopInfo=new ShopInfo(id,imagePath,name,price);
    141 
    142                 list.add(shopInfo);
    143 
    144             }
    145 
    146             et_3.setText(list.toString());
    147 
    148 
    149         }
    150         catch (Exception e)
    151         {
    152             e.printStackTrace();
    153         }
    154 
    155 
    156     }
    157 
    158 
    159     //Gson转集合
    160     public void bt4_OnClick(View v)
    161     {
    162         //转集合
    163         String jsonstr="[{"id":3, "name":"大虾1", "price":12.3, " +
    164                 ""imagePath":"http://192.168.10.165:8080/f1.jpg"}," +
    165                 "{"id":4, "name":"大虾2", "price":12.5, " +
    166                 ""imagePath":"http://192.168.10.165:8080/f2.jpg"}]";
    167 
    168         List<ShopInfo> list=new Gson().fromJson(jsonstr, new TypeToken<List<ShopInfo>>() {
    169         }.getType());
    170 
    171         et_3.setText(list.toString());
    172 
    173 
    174     }
    175 
    176 
    177     //Gson对象转JSON
    178     public void bt5_OnClick(View v)
    179     {
    180         //对象转JSON
    181         ShopInfo info=new ShopInfo(3,"http://www.sina.com","KK",1000);
    182 
    183         String json=new Gson().toJson(info);
    184 
    185         et_3.setText(json);
    186 
    187 
    188     }
    189 
    190     //Gson集合转JSON
    191     public void bt6_OnClick(View v)
    192     {
    193         //集合转JSON
    194         List<ShopInfo> list=new ArrayList<ShopInfo>();
    195 
    196         list.add(new ShopInfo(3, "http://www.sina.com","KK", 1000));
    197         list.add(new ShopInfo(4, "http://www.sina.com/cn", "KK1", 2000));
    198         String json=new Gson().toJson(list);
    199 
    200         et_3.setText(json);
    201 
    202 
    203     }
    204 
    205 
    206 }
    .java

  • 相关阅读:
    HDU Problem 1811 Rank of Tetris【拓扑排序+并查集】
    POJ Problem 2367 Genealogical tree【拓扑排序】
    HDU Problem 2647 Reward【拓扑排序】
    HDU Problem 1285 确定比赛名次【拓扑排序】
    HDU Problem HDU Today 【最短路】
    HDU Problem 3665 Seaside【最短路】
    HDU Problem 一个人的旅行 【最短路dijkstra】
    HDU Problem 1596 find the safest road【最短路dijkstra】
    Beyond Compare文本合并进行内容替换要注意什么
    用这些工具都可以比较代码的差异
  • 原文地址:https://www.cnblogs.com/arxk/p/5588916.html
Copyright © 2011-2022 走看看