再识json

json(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。

JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,用于提升网络传输速率。

json简单点儿说就是JavaScript的对象和数组

     现在看了上面三行,觉得当时对json的理解太浅了。以前面试,被问到你知道json吗?我觉得提问人小看人心想不就是一个数组里面装着几个对象嘛! 直到遇见问卷这类问题才发现原来json作用如此之大 也更深层次了解。

    为什么说json是传送数据的神器,提问者的意图有看看面试者有没有面向对象思想。

    组长让我向数据库中录入一套问卷,供前端调用处理。 第一反应:问卷存成一个String放在问卷表的一个字段中, 第二次优化:表中有问卷id与名称。其余的就剩下问题了

    打算每个题目用&@@&分隔;也确实这么做了,给了前端,前端哭了。 java是面向对象的,把问卷转化成一个json串传过去。于是开始构造json这个时候便接触到了

JSONObject与JSONArray两个类都继承Object

1.Map<String, Object> outJson1_1 = new HashMap<String, Object>();
  System.out.println("Map<String, Object>"+":"+outJson1_1);

输出: Map<String, Object>:{}

2.JSONObject jso1=new JSONObject();
    jso1.put("score", 4);
    jso1.put("options", "无明显不适");
    jso1.put("id", 1.1);
   System.out.println("JSONObject"+":"+jso1);
 输出: JSONObject:{"score":4,"options":"无明显不适","id":1.1}

3.JSONObject jso1=new JSONObject();
    JSONObject jso2=new JSONObject();
    JSONArray array4=new JSONArray();
    jso1.put("score", 4);
    jso1.put("options", "无明显不适");
    jso1.put("id", 1.1);
    array4.add(jso1);
    jso2.put("score", 2);
    jso2.put("options", "时常疲倦不堪");
    jso2.put("id", 1.2);
    array4.add(jso2);
  System.out.println("JSONArray"+":"+array4);
输出: JSONArray:[{"score":4,"options":"无明显不适","id":1.1},{"score":2,"options":"时常疲倦不堪","id":1.2}]

4.Map<String, Object> outJson1_1 = new HashMap<String, Object>();
    outJson1_1.put("score", 4);
    outJson1_1.put("options", "无明显不适");
    outJson1_1.put("id", 1.1);
    System.out.println("Map<String, Object>"+":"+outJson1_1);
输出: Map<String, Object>:{score=4, options=无明显不适, id=1.1}  
5.JSONArray array1=new JSONArray();
    Map<String, Object> outJson1_1 = new HashMap<String, Object>();
    Map<String, Object> outJson1_2 = new HashMap<String, Object>();
    outJson1_1.put("score", 4);
    outJson1_1.put("options", "无明显不适");
    outJson1_1.put("id", 1.1);
    array1.add(outJson1_1);
    outJson1_2.put("score", 2);
    outJson1_2.put("options", "时常疲倦不堪");
    outJson1_2.put("id", 1.2);
    array1.add(outJson1_2);
  System.out.println("array1"+":"+array1);
输出: array1:[{"score":4,"options":"无明显不适","id":1.1},{"score":2,"options":"时常疲倦不堪","id":1.2}]
   
  JSONObject jSONArray = JSONObject.fromObject(outJson);
  System.out.println("jSONArray"+":"+jSONArray);
  out.write(jSONArray.toString());  
 很容易理解,如果是数组就用
JSONArray array1=new JSONArray();
如果是对象就用
JSONObject jso1=new JSONObject();
  这样便可以随意构造自己想要的格式。以上输出可以看出
JSONObject jso1=new JSONObject();与 Map<String, Object> outJson1_1 = new HashMap<String, Object>();
得到的结果相同,应该存在快慢的区别吧.
以上是构建json串返回给前端,给他一套问卷,那么用户做过之后他又要返回给我一套用户做过的json串下回分析解析他传过来的json串。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
调用API实现图像搜索功能通常涉及到向一个提供图像识别服务的服务器发送HTTP请求,并获取搜索结果。Python中可以通过`requests`库来发送这些请求。以下是一个简化的例子,展示了如何使用Python调用一个API来实现图像搜索的基本流程。 首先,确保安装了`requests`库,如果没有安装,可以使用pip安装: ```bash pip install requests ``` 然后,可以编写如下代码调用一个API进行图像搜索: ```python import requests from PIL import Image from io import BytesIO # 假设这是提供图像搜索服务的API的URL api_url = "https://api.example.com/image-search" # 需要上传的图像文件路径 image_path = "path_to_your_image.jpg" # 打开图像文件 with open(image_path, 'rb') as f: image_content = f.read() # 创建一个Image对象,用于处理图像文件 image = Image.open(BytesIO(image_content)) # 将图像文件编码为base64字符串 import base64 encoded_image = base64.b64encode(image_content).decode('utf-8') # 准备请求数据,这里假设API接受base64编码的图像作为搜索目标 # 实际上API可能有不同的要求,需要根据具体的API文档来准备数据 payload = { 'image': encoded_image } # 设置请求头部,例如API密钥和内容类型等 headers = { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' } # 发送POST请求到API response = requests.post(api_url, json=payload, headers=headers) # 检查响应状态码,如果成功则处理结果 if response.status_code == 200: results = response.json() print("Image search results:", results) else: print("Failed to search image:", response.status_code, response.text) ``` 这段代码是基于假设的API和参数。在实际使用中,你需要替换为真实的API URL、认证方式、请求参数等。 请注意,实际的API可能需要不同的认证方式、请求方法(如GET或POST)、请求头、请求参数和响应格式。因此,最好的做法是查阅你所使用的图像搜索服务API的官方文档来了解正确的使用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值