serverJson Supoort 中文编码
This commit is contained in:
		
							parent
							
								
									4ecb9cc30b
								
							
						
					
					
						commit
						e47a147c3b
					
				| @ -214,13 +214,16 @@ func (c *Controller) Abort(code string) { | ||||
| 	panic(code) | ||||
| } | ||||
| 
 | ||||
| func (c *Controller) ServeJson() { | ||||
| func (c *Controller) ServeJson(encoding ...bool) { | ||||
| 	content, err := json.MarshalIndent(c.Data["json"], "", "  ") | ||||
| 	if err != nil { | ||||
| 		http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) | ||||
| 		return | ||||
| 	} | ||||
| 	c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=UTF-8") | ||||
| 	if len(encoding) > 0 && encoding[0] == true { | ||||
| 		content = []byte(stringsToJson(string(content))) | ||||
| 	} | ||||
| 	c.writeToWriter(content) | ||||
| } | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										14
									
								
								utils.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								utils.go
									
									
									
									
									
								
							| @ -232,3 +232,17 @@ func ParseForm(form url.Values, obj interface{}) error { | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func stringsToJson(str string) string { | ||||
| 	rs := []rune(str) | ||||
| 	jsons := "" | ||||
| 	for _, r := range rs { | ||||
| 		rint := int(r) | ||||
| 		if rint < 128 { | ||||
| 			jsons += string(r) | ||||
| 		} else { | ||||
| 			jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json | ||||
| 		} | ||||
| 	} | ||||
| 	return jsons | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user