搜索词>>JSON 耗时0.1280
  • java json字符串转对象_json转换为java对象_ json字符串转对象数组使用JAXB

    java json字符串转对象_json转换为java对象_ json字符串转对象数组java json字符串转对象_json转换为java对象_    json字符串转对象数组
  • 使用Jackson写巨大的JSON文件

    使用Jackson写巨大的JSON文件使用Jackson写巨大的JSON文件
  • MySQL中JSON数据类型的操作和分析

    说明:json除了键值对的形式以外,还包括Array数列形式,这在mysql5.7及以上也是支持的,对Array中元素的操作相当于直接对键值对中的值操作,少了“键”的定位这一步,这里不单独展示,所有的函数都都同时支持,大同小异MySQL中JSON数据类型的操作和分析
  • handlerexceptionresolver 返回json

    handlerexceptionresolver 返回json  <pre> <code class="language-java">package net.xqlee.security.interceptor; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; @Component public class SecurityHandlerExceptionResolver implements HandlerExceptionResolver { private static Logger logger = LoggerFactory.getLogger(SecurityHandlerExceptionResolver.class); private static String data = "data"; private static String code = "code"; private static String msg = "msg"; private static String date = "date"; @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { // 未登陆异常捕获 // 返回错误信息 Map<String, Object> resultMap = new HashMap<>(); if (exception instanceof AuthenticationCredentialsNotFoundException) { logger.info("User Not Login:" + request.getRequestURL()); resultMap.put(code, "00001"); resultMap.put(msg, "用户未登录"); resultMap.put(data, ""); } else if (exception instanceof AccessDeniedException) { logger.info("Access Denied [ID:" + SecurityContextHolder.getContext().getAuthentication().getName() + "]:" + request.getRequestURL()); resultMap.put(code, "00001"); resultMap.put(msg, "用户权限不够"); resultMap.put(data, ""); } //这里是核心,用jsonview方式返回 MappingJackson2JsonView jackson2JsonView = new MappingJackson2JsonView(); //设置返回的json map jackson2JsonView.setAttributesMap(resultMap); ModelAndView view = new ModelAndView(); view.setView(jackson2JsonView); return view; } } </code></pre>  <br /> <br /> 返回信息: <pre> <code class="language-json">{ "msg": "用户未登录", "data": "", "code": "00001" }</code></pre>  
  • Java EE 8 JSON Pointer讲解

    Java EE 8 JSON Pointer讲解,Java EE 8包含JSON处理API的更新,并为最新的JSON标准提供最新的IEFT标准。Java EE 8包含JSON处理API的更新,并为最新的JSON标准提供最新的IEFT标准。他们是: <ul> <li>JSON Pointer (RFC 6901)</li> <li>JSON Patch (RFC 6902)</li> <li>JSON Merge Patch (RFC 7396)</li> </ul>   <h2 style="margin-left:0px; margin-right:0px; text-align:start">入门</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">要开始使用JSON-P,您需要Maven中央存储库中的以下依赖项。</span></span></span></p> <pre> <code class="language-xml"><dependency> <groupId>javax.json</groupId> <artifactId>javax.json-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> <version>1.1</version> </dependency></code></pre> <h2>JSON-Pointer</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">JSON指针定义了一个字符串表达式,该表达式引用JSON文档的层次结构内的元素。通过JSON指针表达式,您可以通过检索,添加,删除和替换由表达式引用的元素或值来访问和操作JSON文档。</span></span></span></p> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">入口API是<em><strong>javax.json.JsonPointer</strong></em>接口。通过调用<em><strong>javax.json.Json</strong></em>  类上的静态工厂方法<strong><em>createPointer(String expression)</em></strong>并将其传递给指针表达式来创建实例。</span></span></span></p> <h3 style="margin-left:0px; margin-right:0px; text-align:start">检索一个值</h3> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">如果下面给出了JSON文档,并且您想要检索<strong><em>title</em></strong>元素的值,则可以创建JSON指针表达式  <em><strong>/title</strong></em>。</span></span></span></p> <pre> <code class="language-json">{ "title": "Java EE: Only What's New", "author": "Alex Theedom", "chapters": [ "Chapter 1: Java EE 8 What’s New Overview", "Chapter 2: Java API for JSON Binding 1.0 (JSR 367)", "Chapter 3: Java EE Security API 1.0 (JSR 375)" ], "released": true, "pages": 300, "sourceCode": { "repositoryName": "Java-EE-8-Only-Whats-New", "url": "github.com/readlearncode/" }, "otherBooks": [ { "title": "Professional Java EE Design Patterns", "length": 350 } ] } </code></pre> <pre> <code class="language-java">JsonObject jsonObject = ... create JSONObject from JSON document ...;</code></pre> 下面的代码片段创建一个<strong><em>JsonPointer</em> </strong>并引用<em><strong>title</strong> </em>元素。然后它调用传递<em><strong>JsonObject</strong></em>来查询的<strong><em>getValue()</em></strong>方法。 <pre> <code class="language-java">JsonValue jsonValue = Json.createPointer(“/ title”)。getValue(jsonObject);</code></pre> <h3 style="margin-left:0px; margin-right:0px; text-align:start">添加一个值</h3> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">要向JSON文档添加(或插入)值,请遵循与检索相同的逻辑,方法是使用JSON指针表达式来标识文档中的插入点。以下代码片段将新的<em><strong>“category”:“</strong></em></span></span></span><em><strong>Programming</strong></em><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff"><em><strong>”</strong></em> JSON对象添加到文档的根目录。</span></span></span></p> <pre> <code class="language-java">JsonObject jsonObject = Json .createPointer("/category") .add(jsonObject, Json.createValue("Programming"));</code></pre> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">返回的<em><strong>JsonObject</strong> </em>是整个新的修改对象。</span></span></span></p> <h3 style="margin-left:0px; margin-right:0px; text-align:start">删除一个值</h3> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">删除过程需要将删除的值的位置表示为JSON指针表达式。下面的代码片段删除<em><strong>title</strong> </em>元素,并将修改的JSON文档作为<em><strong>JsonStructure</strong> </em>实例返回</span></span></span></p> <pre> <code class="language-java">JsonStructure jsonStructure = Json.createPointer(“/ title”)。remove(jsonObject);</code></pre> <h3 style="margin-left:0px; margin-right:0px; text-align:start">替换一个值</h3> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">要替换要替换的元素的JSON指针表达式,并将替换元素传递给<em><strong>replace()</strong></em>方法。下面的代码片段替换<strong><em>title</em> </strong>元素的值并返回修改后的JSON文档。</span></span></span></p> <pre> <code class="language-java">JsonStructure jsonStructure = Json .createPointer("/title") .replace(jsonObject, Json.createValue("Java EE 8"));</code></pre> <h3 style="margin-left:0px; margin-right:0px; text-align:start">测试一个值</h3> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff">可以使用<em><strong>containsValue()</strong> </em>方法测试某个位置存在的值。下面的代码片段测试以查看它在由JSON指针表达式<em><strong>/ doesNotExist</strong></em>表示的位置处有一个值。</span></span></span></p> <pre> <code class="language-java">Boolean containsValue = Json .createPointer("/doesNotExist") .containsValue(jsonObject);</code></pre> <h2 style="margin-left:0px; margin-right:0px; text-align:start">总结结</h2> <p style="margin-left:0px; margin-right:0px; text-align:start"><span style="font-family:"Noto Sans",sans-serif"><span style="color:#000000"><span style="background-color:#ffffff"><span style="font-family:inherit">上面这些,这就是关于JSON Processing新功能的第一篇文章。</span></span></span></span></p>
  • Form 获取JSON参数和URL参数

    Form前置准备首先我们有一个表单<form id="params">l; <input name="userName" value="123">l; <input name="userSex" value="男">l; </form>l;方式Form前置准备首先我们有一个表单<form id="params"> <input name="userName" value="123"> <input name="userSex" value="男"> </form>方式一:获取URL参数,用问号连接let params=$('#params').serialize(); //使用参数 let reqUrl='xxx.com/xxx.do?'+params实际得params内容格式为:userName=123&userSex=男方式二:获取JSON参数首先需要一个工具方法function getFormToJson(formObj){ let formData=$(formObj).serializeArray(); var obj={} for (var i in formData) { obj[formData[i].name]=formData[i]['value']; } return obj; }具体使用:let params=getFormJSON($('#params'));上述params参数实际为:{ "userName":"123", "userSex":"男" }
  • GSON 将JSON数组解析为Java数组或集合(List)

    前言值得一提的是JSON只有数组数据类型前言值得一提的是JSON只有数组数据类型。Java同时具有 - 数组和列表。学习使用Google GSON库将包含json数组的JSON反序列化或解析为java 数组或java列表对象。​GSON1.将JSON数组解析为根对象$title(user.json) [ { "name": "Alex", "id": 1 }, { "name": "Brian", "id": 2 }, { "name": "Charles", "id": 3 } ]$title(user.java) public class User { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }1.1。对象数组用于将json数组反序列化为root的 Java程序- 对象的Java数组。String userJson = "[{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]"; Gson gson = new Gson(); User[] userArray = gson.fromJson(userJson, User[].class); for(User user : userArray) { System.out.println(user); }输出:User [id=1, name=Alex] User [id=2, name=Brian] User [id=3, name=Charles]1.2。对象列表用于将json数组反序列化为root的 Java程序- 到Java对象列表。import java.lang.reflect.Type; import java.util.ArrayList; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; String userJson = "[{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]"; Gson gson = new Gson(); Type userListType = new TypeToken<ArrayList<User>>(){}.getType(); ArrayList<User> userArray = gson.fromJson(userJson, userListType); for(User user : userArray) { System.out.println(user); }输出:User [id=1, name=Alex] User [id=2, name=Brian] User [id=3, name=Charles]2.将JSON数组解析为成员如果JSON数组是非根对象,它们会毫无困难地将JSON数组解析为成员。我们可以fromJson()以通常的方式使用该方法,它将正确地将json数组解析为所需的java数组或列表。$title(department.json) { "id" : 1, "name" : "HR", "users" : [ { "name": "Alex", "id": 1 }, { "name": "Brian", "id": 2 }, { "name": "Charles", "id": 3 } ]2.1。成员数组将json数组反序列化为成员对象的 Java程序- 作为成员字段的Java对象数组。$title(Department.java) public class Department { private long id; private String name; private User[] users; //Getters and Setters @Override public String toString() { return "Department [id=" + id + ", name=" + name + ", users=" + Arrays.toString(users) + "]"; } }demoString departmentJson = "{'id' : 1, " + "'name': 'HR'," + "'users' : [" + "{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]}"; Gson gson = new Gson(); Department department = gson.fromJson(departmentJson, Department.class); System.out.println(department);输出:Department [id=1, name=HR, users=[User [id=1, name=Alex], User [id=2, name=Brian], User [id=3, name=Charles]]]2.2。会员名单Java程序将json数组反序列化为成员对象 - 将Java对象列为成员字段。public class Department { private long id; private String name; private List<User> users; //Getters and Setters @Override public String toString() { return "Department [id=" + id + ", name=" + name + ", users=" + users + "]"; } }String departmentJson = "{'id' : 1, " + "'name': 'HR'," + "'users' : [" + "{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]}"; Gson gson = new Gson(); Department department = gson.fromJson(departmentJson, Department.class); System.out.println(department);输出:Department [id=1, name=HR, users=[User [id=1, name=Alex], User [id=2, name=Brian], User [id=3, name=Charles]]]​​​​​​​
  • httpclient4.5使用详解 httpclient 4.5 post传递json参数

    httpclient4.5使用详解 httpclient 4.5 post传递json参数httpclient4.5使用详解 httpclient 4.5 post传递json参数 <pre> <code class="language-java">import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * 工具类 httpclient4.5 * * @author xq * */ public class HttpclientUtils { /** * post请求 json参数 * * @param url * @param bodyJsonParams * @param headers * @return * @throws IOException */ public static String doPost(String url, String bodyJsonParams, Map<String, String> headers) throws IOException { HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(bodyJsonParams)); if (headers != null && headers.keySet().isEmpty()) { Set<String> keySet = headers.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = iterator.next(); String value = headers.get(key); httpPost.addHeader(key, value); } } return execute(httpPost); } /** * post k-v参数 * * @param url * @param params * @param headers * @return * @throws IOException */ public static String doPost(String url, Map<String, String> params, Map<String, String> headers) throws IOException { HttpPost httpPost = new HttpPost(url); if (params != null && params.keySet().isEmpty()) { List<NameValuePair> list = new ArrayList<>(); Set<String> keySet = headers.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = iterator.next(); String value = headers.get(key); list.add(new BasicNameValuePair(key, value)); } httpPost.setEntity(new UrlEncodedFormEntity(list)); } if (headers != null && headers.keySet().isEmpty()) { Set<String> keySet = headers.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = iterator.next(); String value = headers.get(key); httpPost.addHeader(key, value); } } return execute(httpPost); } /** * get请求 * * @param url * @param params * @param headers * @return * @throws IOException * @throws ClientProtocolException */ public static String doGet(String url, Map<String, String> params, Map<String, String> headers) throws IOException { // 参数 StringBuilder paramsBuilder = new StringBuilder(url); if (params != null && params.keySet().isEmpty()) { if (url.indexOf("?") == -1) { paramsBuilder.append("?"); } List<NameValuePair> list = new ArrayList<>(); Set<String> keySet = headers.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = iterator.next(); String value = headers.get(key); list.add(new BasicNameValuePair(key, value)); } String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(list)); paramsBuilder.append(paramsStr); } HttpGet httpGet = new HttpGet(paramsBuilder.toString()); // 头 if (headers != null && headers.keySet().isEmpty()) { Set<String> keySet = headers.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = iterator.next(); String value = headers.get(key); httpGet.addHeader(key, value); } } return execute(httpGet); } /** * 执行请求并返回string值 * * @param httpUriRequest * @return * @throws IOException */ private static String execute(HttpUriRequest httpUriRequest) throws IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { CloseableHttpResponse response = httpClient.execute(httpUriRequest); if (response.getStatusLine().getStatusCode() == 200) {// 请求成功状态 try (BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(response.getEntity().getContent()))) { String result=""; String tmp=null; while((tmp=bufferedReader.readLine())!=null){ result+=tmp; } return result; } } } return null; } } </code></pre>  
  • Vue中axios设置Content-Type='application/json;charset=UTF-8'无效

    在vue2.0中使用了axios库,设置请求头Content-Type='application/json;charset=UTF-8'无效axios.defaults.headers.common['Content-Type'] = 'a在vue2.0中使用了axios库,设置请求头Content-Type='application/json;charset=UTF-8'无效axios.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8' 还尝试了http.get(http.api.url, { params: params, headers: { 'Content-Type': 'application/json;charset=UTF-8' } }) 也是没有起作用。解决办法://use params instead of data//用 params 代替 data完整写法如下:axios({ method: 'post', url: '/my/api', headers: { 'Content-type': 'application/json;charset=UTF-8' }, params: { 'grant_type': 'code', 'client_id': '1231453', 'client_secret': 'THIS_IS_THE_SECRET' } }) .then((response) => { console.log(response); }) .catch((error) => { console.log(error); } );  
  • GSON 序列化与反序列化使用说明

    前言学习使用Google GSON库将Java对象序列化为JSON表示形式,并将JSON字符串反序列化为等效的Java对象前言学习使用Google GSON库将Java对象序列化为JSON表示形式,并将JSON字符串反序列化为等效的Java对象。GSON提供简单的toJson()和fromJson()方法将Java对象转换为JSON,反之亦然。​GSON使用GsonBuilder创建Gson具有自定义配置的对象,例如漂亮的打印。//Gson gson = new Gson(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); Employee emp = new Employee(1001, "Lokesh", "Gupta", "howtodoinjava@gmail.com"); String jsonString = gson.toJson(emp); Employee empObject = gson.fromJson(jsonString, Employee.class);1.依赖性Maven依赖。访问maven存储库以获取最新版本。$title(pom.xml) <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency>Gradle依赖。<dependency> dependencies { implementation 'com.google.code.gson:gson:2.8.5' }2.序列化 - 使用Gson编写JSONGson上下文中的序列化意味着将Java对象转换为其JSON表示。为了进行序列化,我们需要一个处理转换的Gson对象。接下来,我们需要调用函数toJson()并传递该Employee对象。Employee emp = new Employee(1001, "Lokesh", "Gupta", "howtodoinjava@gmail.com"); Gson gson = new Gson(); String jsonString = gson.toJson(emp); System.out.println(jsonString);程序输出:{ "id":1001, "firstName":"Lokesh", "lastName":"Gupta", "email":"howtodoinjava@gmail.com" }2.反序列化 - 使用Gson读取JSONGson上下文中的反序列化意味着将JSON字符串转换为等效的Java对象。为了进行反序列化,我们需要一个Gson对象并从json()调用函数并在解析完成后传递两个参数,即JSON字符串和期望的java类型。String jsonString = "{'id':1001, 'firstName':'Lokesh', 'lastName':'Gupta', 'email':'howtodoinjava@gmail.com'}"; Gson gson = new Gson(); Employee empObject = gson.fromJson(jsonString, Employee.class); System.out.println(empObject);程序输出:Employee [id=1001, firstName=Lokesh, lastName=Gupta, email=howtodoinjava@gmail.com]代码段 小部件