为什么我的JSON.parse失败了?(Why is my JSON.parse failing? Ruby on Rails)
我正在尝试将Flickr的API中的图像加载到Ruby on Rails应用程序中,但我在JSON.parse()行上获得了“Unexpected Token”。
我在这里找到了另一个响应,其中返回的JSON将其双引号转义出来,解决方案是将.gsub事件添加到最后,但我仍然收到错误。
谁知道问题是什么?
def add @jsonresults = open("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=bb398c11934abb6d51bdd720020f6a4a&per_page=1&page=1&format=json&nojsoncallback=1").read @images = JSON.parse(@jsonresults.to_json.gsub('\"', '"')) end
错误:
JSON::ParserError in ImagesController#add 757: unexpected token at '"{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}"'
I'm trying to load images from Flickr's API into a Ruby on Rails app, but I'm getting "Unexpected Token" on my JSON.parse() line.
I found another response here where the returned JSON had it's double quotes escaped out, and the solution was to add the .gsub thing to the end, but I'm still getting an error.
Anyone know what the problem is?
def add @jsonresults = open("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=bb398c11934abb6d51bdd720020f6a4a&per_page=1&page=1&format=json&nojsoncallback=1").read @images = JSON.parse(@jsonresults.to_json.gsub('\"', '"')) end
The error:
JSON::ParserError in ImagesController#add 757: unexpected token at '"{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}"'
原文:https://stackoverflow.com/questions/13673809
最满意答案
为了以最简单的方式获取数据,我使用此代码重新命令:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference blogRef = rootRef.child("Blog"); ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot ds : dataSnapshot.getChildren()) { String desc = ds.child("desc").getValue(String.class); String title = ds.child("title").getValue(String.class); String image = ds.child("image").child("path").getValue(String.class); Log.d("TAG", desc + " / " + title + " / " + image); } } @Override public void onCancelled(DatabaseError databaseError) {} }; blogRef.addListenerForSingleValueEvent(eventListener);
你的输出将是:
tst / test / /v0/b/infectionui...
正如你所见,我已经使用了String类。 这也是您需要在
FirebaseRecyclerAdapter
。如果你想从图像获得antoher值,只需将
.child("path")
改为另一个孩子,让我们说.child("schemeSpecificPart")
。To get that data in the simplest way, i recomand you using this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference blogRef = rootRef.child("Blog"); ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot ds : dataSnapshot.getChildren()) { String desc = ds.child("desc").getValue(String.class); String title = ds.child("title").getValue(String.class); String image = ds.child("image").child("path").getValue(String.class); Log.d("TAG", desc + " / " + title + " / " + image); } } @Override public void onCancelled(DatabaseError databaseError) {} }; blogRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
tst / test / /v0/b/infectionui...
As you see, i have used String class. This is what you need to use also in your
FirebaseRecyclerAdapter
.If you want to get antoher value from image, just change
.child("path")
with another child, let's say.child("schemeSpecificPart")
.
相关问答
更多-
这是因为你得到的数据是'HashMap'的形式,而不是'UserModel'。你可以这样做来创建对象。 UserModel userModel = dataSnapshot.getValue(UserModel.class); 编辑:如果有多个用户,您还需要循环浏览子项。 喜欢这个。 for(Datasnapshot userSnapshot : dataSnapsot.getChildren(){ UserModel userModel = userSnapshot.getValue(UserMo ...
-
无法将java.util.HashMap类型的值转换为String(Failed to convert value of type java.util.HashMap to String)[2023-05-31]
您将String的 HashMap作为键,而Object作为Value,因此将其映射到类似下面的内容。 myRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Mapmap = (Map ) dataSnap ... -
在Controller类级别提供@RestController,在方法级别提供@ResponseBody 。 确保所有与杰克逊相关的罐子都在类路径中。 将jackson相关的依赖项添加到您的pom.xml中 @RequestMapping(value = "/user",method = RequestMethod.GET) @ResponseBody public ResponseEntity
-
为了以最简单的方式获取数据,我使用此代码重新命令: DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference blogRef = rootRef.child("Blog"); ValueEventListener eventListener = new ValueEventListener() { @Override public void onDataChange ...
-
创建一个简单的pojo Class First class MyClass { @JsonProperty private String Name; @JsonProperty private String CreationDate; } 并使用此代码... byte[] mapData = Files.readAllBytes(Paths.get("process.txt")); ObjectMapper objectMapper=new ObjectMapper(); //add this line ...
-
如何编组/解组ContentValues以将通用类型插入到ContentProvider中? Location loc = mLocationClient.getLastLocation(); HashMap hm = new HashMap(); hm.put("LOCATIONS", loc); Parcel myParcel = Parcel.obtain(); myParcel.writeMap(hm); myParcel.setDataPosition(0); ContentValues ...
-
可以使用模型类来实现此目的,或者您可以使用String类使用最简单的方法。 为此,您可以使用以下代码: DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference usersRef = rootRef.child("Accounts").child("Users"); ValueEventListener eventListener = new ValueEventListener( ...
-
Davies Liu在Spark用户邮件列表中发现了这个问题 。 我错过了Scala和Python API之间的细微差别。 您必须传入模式字符串(例如“overwrite”)作为Python API中的第3个参数,而不是Scala API。 如下更改语句可解决此问题: df.write.jdbc("jdbc:sqlserver://serverURL", "dbName.SparkTestTable1", "overwrite", dict(driver="com.microsoft.sqlserver.j ...
-
错误SessionMap无法强制转换为java.util.HashMap(error SessionMap cannot be cast to java.util.HashMap)[2022-01-21]
这是一个SessionAware实现,使用它 private Mapsession; @Override public void setSession(Map session) { this.session = session; } 在execute创建一个带有值的新集合并放入会话映射中。 您的代码的问题是重用最初用于构建查询的相同变量abc 。 您应该创建另一个并使用List而不是Map来保持要迭代并在JSP中显示的项的顺序。 最好创 ...