fastJson在把json格式的字符串转换成JSONObject的时候,使用的是HashMap,所以排序规则是根据HASH值排序的,如果想要按照字符串顺序遍历JSON属性,需要在转换的时候指定使用LinkedHashMap代替HashMap。 以下为实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) { String jsonStr = "{\"size\":\"7.5\",\"width\":\"M (B)\"}"; System.out.println("无序遍历结果:"); JSONObject jsonObj = JSON.parseObject(jsonStr); for (Map.Entry<String, Object> entry : jsonObj.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println("-------------------"); System.out.println("有序遍历结果:"); LinkedHashMap<String, String> jsonMap = JSON.parseObject(jsonStr, new TypeReference<LinkedHashMap<String, String>>() { }); for (Map.Entry<String, String> entry : jsonMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } |
from:https://www.cnblogs.com/seely/p/5715512.html
View Details遍历JsonArray
1 2 3 4 5 6 7 8 9 10 11 12 |
// 一个未转化的字符串 String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ; // 首先把字符串转成 JSONArray 对象 JSONArray json = JSONArray.fromObject(str ); if(json.size()>0){ for(int i=0;i<json.size();i++){ // 遍历 jsonarray 数组,把每一个对象转成 json 对象 JSONObject job = json.getJSONObject(i); // 得到 每个对象中的属性值 System.out.println(job.get("name")+"=") ; } } |
遍历JsonObject
1 2 3 4 5 6 7 8 |
JSONObject jsonObject = new JSONObject(s); //然后用Iterator迭代器遍历取值,建议用反射机制解析到封装好的对象中 JSONObject jsonObject = new JSONObject(jsonString); Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ key = (String) iterator.next(); value = jsonObject.getString(key); } |
from:https://blog.csdn.net/changhenshui1990/article/details/69950663/
View DetailsGUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值。 GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。例如:76895313-839E-4E89-BAFC-B253BFF3173F 世界上的任何两台计算机都不会生成重复的 GUID 值。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。在 Windows 平台上,GUID 应用非常广泛:注册表、类及接口标识、数据库、甚至自动生成的机器名、目录名等。 1.SQL Server数据库 以前开发SQL Server数据库将表定义中将列类型指定为uniqueidentifier,则列的值就为 GUID 类型。 2.使用T-SQL生产一个GUID insert into table1(id,name,…) values(NewID(),’张三',…) 3.在C#中创建一个GUID Guid guid = Guid.NewGuid(); Console.Writeln(guid.ToString()); 4.在Java中创建UUID 在网上查资料才知道在Java中,变成了UUID。创建方式也出奇简单System.out.println( java.util.UUID.randomUUID()); 摘自:http://qfqf16.blog.163.com/blog/static/128109527201272610564410/ from:https://www.cnblogs.com/shirley-1019/archive/2013/07/31/3227671.html
View Details由于 web api 项目通常是被做成了一个独立站点,来提供数据,在做web api 项目的时候,不免前端会遇到跨域访问接口的问题。 刚开始没做任何处理,用jsonp的方式调用 web api 接口,总是报一个错误,如下: 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的:
1 |
{"YourSignature":"嫁人要嫁程序员,钱多话少死得早"} |
然而,JSONP请求期望得到这样的JSON:
1 |
jQuery123456([{"YourSignature":"嫁人要嫁程序员,钱多话少死得早"}]) |
所以我们需要对WebAPI做拓展,让它支持这样的callback 解决方案如下: 只需要给全局注册一个JsonCallbackAttribute,就可以判断接口的访问是属于跨域,还是非跨域,正常的返回。 因为我们的接口,可能是用来给 移动端(Android 、IOS)做数据接口,也有可能是给网站用,所以,考虑到可能存在跨域的问题。
1 |
GlobalConfiguration.Configuration.Filters.Add(new JsonCallbackAttribute()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class JsonCallbackAttribute : ActionFilterAttribute { private const string CallbackQueryParameter = "callback"; public override void OnActionExecuted(HttpActionExecutedContext context) { var callback = string.Empty; if (IsJsonp(out callback)) { var jsonBuilder = new StringBuilder(callback); jsonBuilder.AppendFormat("({0})", context.Response.Content.ReadAsStringAsync().Result); context.Response.Content = new StringContent(jsonBuilder.ToString()); //context.Response.Content = new StringContent("C(\"a\")"); } base.OnActionExecuted(context); } private bool IsJsonp(out string callback) { callback = System.Web.HttpContext.Current.Request.QueryString[CallbackQueryParameter]; return !string.IsNullOrEmpty(callback); } |
结合下面图片不难开出,请求的地址带回了,callback的参数标识。 测试代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<html> <head> <title>团队信息列表</title> <script type="text/javascript" src="@Url.Content("~/scripts/jquery-1.7.1.js")"></script> </head> <body> <ul id="contacts"></ul> <script type="text/javascript"> $(function () { $.ajax({ Type: "GET", url: "http://app.uni2uni.com/api/CloudService/GetAllGroup", dataType: "jsonp", success: listContacts }); }); function listContacts(contacts) { alert(contacts); $.each(contacts.data, function (index, contact) { var html = "<li><ul>"; html += "<li>GroupName: " + contact.GroupName + "</li>"; html += "<li>GroupPicture:" + contact.GroupPicture + "</li>"; html += "</ul>"; $("#contacts").append($(html)); }); } </script> </body> </html> |
返回接口如下: 相关文章推荐:http://diaosbook.com/Post/2013/12/27/tips-for-aspnet-webapi-cors from:https://www.cnblogs.com/Kummy/p/3767269.html
View Details