我遇到了一个奇怪的问题,我是 Android 和 Json 的新手。我正在使用一个显示 json 响应的 RESTful WCF,如下所示:
{"StatusCode":200,"Message":"","Result":{"Agencies":[],"Areas":[{"id":11,"area_name":"area 10","description":"updated 10","active":true},{"id":12,"area_name":"Area 11","description":"temp 11","active":true},{"id":13,"area_name":"Area 12","description":"temp12","active":true},{"id":14,"area_name":"area 13","description":"temp 13 3","active":true},{"id":7,"area_name":"Area 6","description":"temp6","active":true},{"id":8,"area_name":"area 7","description":"temp7","active":true},{"id":9,"area_name":"area 8","description":"temp 8","active":true},{"id":10,"area_name":"area 9","description":"temp 9","active":true},{"id":1,"area_name":"area1","description":"temp","active":true},{"id":3,"area_name":"area2","description":"temp2","active":true},{"id":4,"area_name":"area3","description":"asdddasdsa","active":true},{"id":5,"area_name":"area4","description":"dasdassad","active":true},{"id":6,"area_name":"area5","description":"temp5","active":true}],"Districts":[{"id":5,"district_name":"District 5","district_code":"1","description":"temp 5","area_id":8,"area_name":"area 7","active":true},{"id":4,"district_name":"district 4","district_code":"2","description":"temp 4","area_id":8,"area_name":"area 7","active":true},{"id":1,"district_name":"district 1","district_code":"1","description":"descrp","area_id":10,"area_name":"area 9","active":true},{"id":2,"district_name":"district 2","district_code":"1","description":"descrp","area_id":10,"area_name":"area 9","active":true},{"id":3,"district_name":"district 3","district_code":"1","description":"temp 3","area_id":5,"area_name":"area4","active":true}],"FacilityOccupacies":[{"id":1,"facility_name":"C","description":"Computer facility","active":true},{"id":2,"facility_name":"CA","description":"Adult correctional facility","active":true},{"id":3,"facility_name":"CJ","description":"Juvenile correctional facility","active":true},{"id":4,"facility_name":"GS","description":"Garage for operations support, including vehicle maintenance, repair or fuel depot","active":true},{"id":5,"facility_name":"GP","description":"Garage for parking","active":true},{"id":6,"facility_name":"I","description":"Light industrial facility","active":true},{"id":7,"facility_name":"L","description":"Library","active":true},{"id":8,"facility_name":"M","description":"Medical facilities such as clinics, infirmaries or hospitals","active":true},{"id":9,"facility_name":"N","description":"Non-categorized facility","active":true},{"id":10,"facility_name":"OM","description":"Office buildings where approximately 75% or more of occupants are senior level managers","active":true},{"id":11,"facility_name":"OS","description":"Office building","active":true},{"id":12,"facility_name":"SP","description":"Primary school","active":true},{"id":13,"facility_name":"SS","description":"Secondary school","active":true},{"id":14,"facility_name":"U","description":"University","active":true},{"id":15,"facility_name":"W","description":"Warehouse","active":true}],"FireSprinkler":[{"id":2,"fire_sprinkler":"N","description":"A lack of a fire sprinkler fire suppression system.","active":true},{"id":3,"fire_sprinkler":"P","description":"A partial fire sprinkler fire suppression system.","active":true},{"id":1,"fire_sprinkler":"Y","description":"A full fire sprinkler fire suppression system.","active":true}],"FoundSources":[{"id":1,"found_source_name":"COP","description":"Certificates of Participation","active":true},{"id":2,"found_source_name":"COSF","description":"Capital Outlay Stabilization Fund","active":true},{"id":6,"found_source_name":"Exempt","description":"Exempt From Rent","active":true},{"id":4,"found_source_name":"GF","description":"Capital Outlay Stabilization Fund","active":true},{"id":5,"found_source_name":"N/A","description":"Not Applicable","active":true},{"id":3,"found_source_name":"PLTO","description":"Private Lease-To-Own","active":true}],"Locations":[{"id":1,"location_name":"location 1","location_code":"1","description":"temp 1","district_id":3,"district_name":"district 3","active":true}],"MarshallSwift":[{"id":1,"class":"A","frame":"Structural steel columns and beams fire proofed with masonry, concrete, plaster, or other incombustible material.","floor":"Concrete or concrete covered steel deck, fire-proofed.","roof":"Concrete or concrete covered steel deck,fire-proofed.","walls":"Non-bearing curtain walls, masonry, concrete, metal, and glass panels, stone.","active":true},{"id":2,"class":"B","frame":"Reinforced concrete columns and beams.","floor":"Concrete or concrete covered steel deck, fire-proofed.","roof":"Concrete or concrete covered steel deck, fire-proofed.","walls":"Non-bearing curtain walls, masonry, concrete, metal, and glass panels, stone.","active":true},{"id":3,"class":"C","frame":"Structural steel columns and beams fire-proofed with masonry, concrete, plaster, or other incombustible material.","floor":"Concrete or concrete covered steel deck, fire-proofed.","roof":"Wood or steel joist, wood or steel deck.","walls":"Brick, concrete block, or tile masonry, tilt-up formed concrete.","active":true},{"id":4,"class":"D","frame":"Wood or steel studs in bearing wall, wood or steel skeleton frame.","floor":"Wood or steel floor joists or slabs on ground.","roof":"Wood or steel joist, wood or steel deck.","walls":"Almost any material except masonry or concrete. May have masonry veneer over steel or wood framing.","active":true},{"id":5,"class":"S","frame":"Steel – No fire protection.","floor":"Concrete","roof":"Steel – No fire protection.","walls":"Steel – No fire protection.","active":true}],"Profiles":[{"id":1,"profile_name":"Administrator","description":"System Administrator","active":true},{"id":2,"profile_name":"Inspector","description":"Building Inspector","active":true}],"ProjectCategories":[{"id":1,"category_name":"Fire/Life Safety","description":"Fire/Life Safety","active":true},{"id":2,"category_name":"HVAC","description":"HVAC","active":true},{"id":3,"category_name":"Infrastructure","description":"Infrastructure","active":true},{"id":4,"category_name":"Interior Renovation","description":"Interior Renovation","active":true},{"id":5,"category_name":"Shell","description":"Shell","active":true},{"id":6,"category_name":"Energy Consv","description":"Energy Consv","active":true},{"id":7,"category_name":"ADA Access","description":"ADA Access","active":true},{"id":8,"category_name":"Building Services","description":"Building Services","active":true},{"id":9,"category_name":"NONE","description":"NONE","active":true}],"Projects":[],"Structures":[]}}
我正在尝试使用 Jackson 1.9.11 将其反序列化为复杂对象。
所以我用
final ObjectMapper mapper = new ObjectMapper();
RawResult<RawCatalogs> oResult = mapper.readValue(strResponse, new TypeReference<RawResult<RawCatalogs>>() {});
其中 RawResult 是一个带有模板的类,而 RawCatalogs 是一个带有嵌套对象的类,它曾经正确地反序列化响应,但现在它在标题中显示错误。
类的定义是这样的(不知道是否需要嵌套类的信息来全面支持?我想我已经很好地映射了它们,只是为了清楚起见而发布重要信息)
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
final public class RawResult<T> {
private final Integer _StatusCode;
private final Boolean _Status;
private final String _Message;
private final T _Result;
public RawResult(Class<T> oClass) throws InstantiationException, IllegalAccessException
{
_StatusCode = null;
_Message = null;
_Result = BuildGeneric(oClass);
_Status = false;
}
@JsonCreator
public RawResult(
@JsonProperty("StatusCode") Integer StatusCode,
@JsonProperty("Message") String Message,
@JsonProperty("Result") T Result)
{
_StatusCode = StatusCode;
_Message = Message;
_Result = Result;
_Status = StatusCode == 200;
}
private T BuildGeneric(Class<T> oClass) throws InstantiationException,
IllegalAccessException
{
return oClass.newInstance();
}
/**
* @return the _StatusCode
*/
@JsonProperty("StatusCode")
public Integer get_StatusCode() {
return _StatusCode;
}
/**
* @return the _Message
*/
@JsonProperty("Message")
public String get_Message() {
return _Message;
}
/**
* @return the _Result
*/
@JsonProperty("Result")
public T get_Result() {
return _Result;
}
/**
* @return the _Status
*/
@JsonIgnore
public Boolean get_Status() {
return _Status;
}
}
public interface ICatalog {
String _Id = null;
String _Description = null;
boolean _Active = false;
/**
* @return the _Id
*/
public String get_Id();
/**
* @return the _Description
*/
public String get_Description() ;
/**
* @return the _Active
*/
public boolean is_Active() ;
}
public abstract class RawCatalog implements ICatalog {
protected final String _Id;
protected final String _Description;
protected final String _Name;
protected final boolean _Active;
public RawCatalog(String Id, String Description,String Name, boolean Active)
{
_Id = Id;
_Description = Description;
_Name = Name;
_Active = Active;
}
@JsonProperty("id")
public String get_Id() {
// TODO Auto-generated method stub
return _Id;
}
@JsonProperty("description")
public String get_Description() {
// TODO Auto-generated method stub
return _Description;
}
@JsonProperty("name")
public String get_Name() {
// TODO Auto-generated method stub
return _Name;
}
@JsonProperty("active")
public boolean is_Active() {
// TODO Auto-generated method stub
return _Active;
}
}
public final class RawAgency extends RawCatalog {
private final String _AgencyCode;
private final String _LocationId;
private final String _LocationName;
private final int _NumberOfStructures;
@JsonCreator
public RawAgency(
@JsonProperty("id") String Id,
@JsonProperty("description") String Description,
@JsonProperty("agency_name") String Name,
@JsonProperty("active") Boolean Active,
@JsonProperty("agency_code") String AgencyCode,
@JsonProperty("get_LocationId") String LocationId,
@JsonProperty("get_LocationName") String LocationName,
@JsonProperty("get_NumberOfStructures") int NumberOfStructures
)
{
super(Id, Description, Name,Active);
_AgencyCode = AgencyCode;
_LocationId = LocationId;
_LocationName = LocationName;
_NumberOfStructures = NumberOfStructures;
}
/**
* @return the _AgencyName
*/
@JsonProperty("agency_name")
@Override
public String get_Name() {
return super.get_Name();
}
/**
* @return the _AgencyCode
*/
@JsonProperty("agency_code")
public String get_AgencyCode() {
return _AgencyCode;
}
/**
* @return the _LocationId
*/
@JsonProperty("location_id")
public String get_LocationId() {
return _LocationId;
}
/**
* @return the _NumberOfStructures
*/
@JsonProperty("number_of_structures")
public int get_NumberOfStructures() {
return _NumberOfStructures;
}
/**
* @return the _LocationName
*/
@JsonProperty("location_name")
public String get_LocationName() {
return _LocationName;
}
}
import java.util.List;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
final public class RawCatalogs {
private final List<RawAgency> _Agencies;
private final List<RawArea> _Areas;
private final List<RawDistrict> _Districts;
private final List<RawFacilityOccupacy> _FacilityOccupacies;
private final List<RawFireSprinkler> _FireSprinklers;
private final List<RawFoundSources> _FoundSources;
private final List<RawLocation> _Locations;
private final List<RawMarshallSwift> _MarshallSwifts;
private final List<RawProfile> _Profiles;
private final List<RawProjectCategory> _ProjectCategories;
private final List<RawProject> _Projects;
private final List<RawStructure> _Structures;
@JsonCreator
public RawCatalogs(
@JsonProperty("Agencies") List<RawAgency> Agencies,
@JsonProperty("Areas") List<RawArea> Areas,
@JsonProperty("Districts") List<RawDistrict> Districts,
@JsonProperty("FacilityOccupacies") List<RawFacilityOccupacy> FacilityOccupacies,
@JsonProperty("FireSprinkler") List<RawFireSprinkler> FireSprinklers,
@JsonProperty("FoundSources") List<RawFoundSources> FoundSources,
@JsonProperty("Locations") List<RawLocation> Locations,
@JsonProperty("MarshallSwift") List<RawMarshallSwift> MarshallSwifts,
@JsonProperty("Profiles") List<RawProfile> Profiles,
@JsonProperty("ProjectCategories") List<RawProjectCategory> ProjectCategories,
@JsonProperty("Projects") List<RawProject> Projects,
@JsonProperty("Structures") List<RawStructure> Structures
)
{
_Agencies = Agencies;
_Areas = Areas;
_Districts = Districts;
_FacilityOccupacies = FacilityOccupacies;
_FireSprinklers = FireSprinklers;
_FoundSources = FoundSources;
_Locations = Locations;
_MarshallSwifts = MarshallSwifts;
_Profiles = Profiles;
_ProjectCategories = ProjectCategories;
_Projects = Projects;
_Structures = Structures;
}
/**
* @return the _Agencies
*/
@JsonProperty("Agencies")
public List<RawAgency> get_Agencies() {
return _Agencies;
}
/**
* @return the _Areas
*/
@JsonProperty("Areas")
public List<RawArea> get_Areas() {
return _Areas;
}
@JsonProperty("Districts")
/**
* @return the _Districts
*/
public List<RawDistrict> get_Districts() {
return _Districts;
}
/**
* @return the _FacilityOccupacies
*/
@JsonProperty("FacilityOccupacies")
public List<RawFacilityOccupacy> get_FacilityOccupacies() {
return _FacilityOccupacies;
}
/**
* @return the _FireSprinklers
*/
@JsonProperty("FireSprinkler")
public List<RawFireSprinkler> get_FireSprinklers() {
return _FireSprinklers;
}
/**
* @return the _FoundSources
*/
@JsonProperty("FoundSources")
public List<RawFoundSources> get_FoundSources() {
return _FoundSources;
}
/**
* @return the _Locations
*/
@JsonProperty("Locations")
public List<RawLocation> get_Locations() {
return _Locations;
}
/**
* @return the _MarshallSwifts
*/
@JsonProperty("MarshallSwift")
public List<RawMarshallSwift> get_MarshallSwifts() {
return _MarshallSwifts;
}
/**
* @return the _Profiles
*/
@JsonProperty("ProjectCategories")
public List<RawProfile> get_Profiles() {
return _Profiles;
}
/**
* @return the _ProjectCategories
*/
@JsonProperty("Profiles")
public List<RawProjectCategory> get_ProjectCategories() {
return _ProjectCategories;
}
/**
* @return the _Projects
*/
@JsonProperty("Projects")
public List<RawProject> get_Projects() {
return _Projects;
}
/**
* @return the _Structures
*/
@JsonProperty("Structures")
public List<RawStructure> get_Structures() {
return _Structures;
}
}
所以我的问题是为什么显示该错误?我在 stackoverflow 上搜索了类似的问题,所有问题似乎都与编码错误有关,但不是相同的代码错误,而且我无法控制 WCF。
实际上,如果我将 Json Respone 复制粘贴到 this page 中,它看起来格式正确。
我也尝试使用 mapper.configure,但 Jsonparser 的所有功能都不起作用
我做错了什么?
最佳答案
我在读取 JSON 文件时遇到了同样的错误。
您要么确保从 RESTful WCF 获得没有初始 0xfeff 的响应,要么在将其发送到 JSON 解析器之前将其剥离。也许 trim() 可以解决问题......
关于java - org.codehaus.jackson.JsonParseException : Unexpected character ('' (code 65279/0xfeff)): expected a valid value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16655349/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai
如何使此根路径转到:“/dashboard”而不仅仅是http://example.com?root:to=>'dashboard#index',:constraints=>lambda{|req|!req.session[:user_id].blank?} 最佳答案 您可以通过以下方式实现:root:to=>redirect('/dashboard')match'/dashboard',:to=>"dashboard#index",:constraints=>lambda{|req|!req.session[:user_id].b