JsonArray和Gson(JsonArray and Gson)
我正在使用的JSON库是Gson。 我很难制定一个有效的数据类型来表示以下JSON字符串:
{ "latestoffers": [ { "id": "4qXleunwNMCKi8M0q0CuMa", "price": "534.99", "firstrecorded_at": 1377808800, "lastrecorded_at": 1382862800, "seller": "Newegg", "availability": "In stock. [BBX: Buy Box]", "currency": "USD" }, { "id": "4xTIQAPySG68IS0CGyOuyO", "price": "582.41", "firstrecorded_at": 1380725000, "lastrecorded_at": 1382862800, "seller": "Beach Audio", "currency": "USD" }, { "id": "5nW67R2V4CwmE8cwaWsawe", "price": "578.04", "firstrecorded_at": 1379524200, "lastrecorded_at": 1379998900, "seller": "Beach Audio", "currency": "USD" } ], "offers_count": 6, "name": "newegg.com", "recentoffers_count": 2, "sku": "N82E16834216463", "url": "http://www.newegg.com/Product/Product.aspx?Item=N82E16834216463" }
我的数据类(到目前为止)如下,它是方法getOfferData() ,我不知道如何完成。 我也不确定JsonArray是否适合使用JSON元素?
static class LatestOffers { Integer offers_count; String name; Integer recentoffers_count; String sku; String url; java.util.List<JsonArray> getOfferData() { List<JsonArray> list = new ArrayList<JsonArray>(); // how do I get parse the 'id', 'price', 'firstrecorded_at' etc. to add them to the ArrayList? return list; } Integer getOffers_count() { return offers_count; } String getName() { return name; } Integer getRecentoffers_count() { return recentoffers_count; } String getSku() { return sku; } String getUrl() { return url; } }
有什么帮助吗? 谢谢。
编辑
结果我不必要地使事情变得复杂,这按预期工作:
static class LatestOffers { List<Offer> latestoffers; List<Offer> getOffer() { return latestoffers; } } static class Offer { private String id; private String price; private long firstrecorded_at; private long lastrecorded_at; private String seller; private String availability; private String currency; String getId() { return id; } String getPrice() { return price; } long getFirstrecorded_at() { return firstrecorded_at; } long getLastrecorded_at() { return lastrecorded_at; } String getSeller() { return seller; } String getAvailability() { return availability; } String getCurrency() { return currency; } }
感谢所有回答的人,我接受了user2762451的回答,因为他是第一个建议使用另一个类来提供数据的人。
The JSON library I'm using is Gson. I'm having difficulty formulating a functioning data type to represent the following JSON string:
{ "latestoffers": [ { "id": "4qXleunwNMCKi8M0q0CuMa", "price": "534.99", "firstrecorded_at": 1377808800, "lastrecorded_at": 1382862800, "seller": "Newegg", "availability": "In stock. [BBX: Buy Box]", "currency": "USD" }, { "id": "4xTIQAPySG68IS0CGyOuyO", "price": "582.41", "firstrecorded_at": 1380725000, "lastrecorded_at": 1382862800, "seller": "Beach Audio", "currency": "USD" }, { "id": "5nW67R2V4CwmE8cwaWsawe", "price": "578.04", "firstrecorded_at": 1379524200, "lastrecorded_at": 1379998900, "seller": "Beach Audio", "currency": "USD" } ], "offers_count": 6, "name": "newegg.com", "recentoffers_count": 2, "sku": "N82E16834216463", "url": "http://www.newegg.com/Product/Product.aspx?Item=N82E16834216463" }
My data class (so far) is as follows, it's the method getOfferData() that I don't how to complete. I'm also not certain whether JsonArray is the appropriate JSON element to be using?
static class LatestOffers { Integer offers_count; String name; Integer recentoffers_count; String sku; String url; java.util.List<JsonArray> getOfferData() { List<JsonArray> list = new ArrayList<JsonArray>(); // how do I get parse the 'id', 'price', 'firstrecorded_at' etc. to add them to the ArrayList? return list; } Integer getOffers_count() { return offers_count; } String getName() { return name; } Integer getRecentoffers_count() { return recentoffers_count; } String getSku() { return sku; } String getUrl() { return url; } }
Any assistance, please? Thank you.
EDIT
Turns out I was unnecessarily complicating things, this works as intended:
static class LatestOffers { List<Offer> latestoffers; List<Offer> getOffer() { return latestoffers; } } static class Offer { private String id; private String price; private long firstrecorded_at; private long lastrecorded_at; private String seller; private String availability; private String currency; String getId() { return id; } String getPrice() { return price; } long getFirstrecorded_at() { return firstrecorded_at; } long getLastrecorded_at() { return lastrecorded_at; } String getSeller() { return seller; } String getAvailability() { return availability; } String getCurrency() { return currency; } }
Thank you to all who answered, I'm accepting the answer of user2762451 as (s)he was the first to suggest the use of another class for the Offer data.
原文:https://stackoverflow.com/questions/21553151
最满意答案
将
NSSegmentedControl
用于分段控件,并使用tablessNSTabView
在视图之间切换。 如果它在你的应用中有意义,你甚至可以将它们绑定到相同的用户默认值,并且在不写一行代码的情况下实现它。Use
NSSegmentedControl
for the segmented control, and a tablessNSTabView
to switch between views. If it makes sense in your app, you could even bind both to the same user default and implement this without writing a single line of code.
相关问答
更多-
得到解决办法,我为UISegmentedControl设置标签。 if let currentFocus = UIScreen.main.focusedView { if let superView = currentFocus.superview { if superView.tag == 11 { } } } 这里是我如何看待我是否正确的分段控制。 Got work around, I set tag for UI ...
-
我想问题是: [addressBox setUserInteractionEnabled:[segment selectedSegmentIndex]==1]; addressBox包含phoneField ,对吗? 这意味着您在整个视图中禁用用户交互及其子视图。 删除此行并单独处理文本字段。 I guess the problem is: [addressBox setUserInteractionEnabled:[segment selectedSegmentIndex]==1]; ad ...
-
@IBAction func segmentedcontrolAction(sender: AnyObject) { if(segmentedcontrol.selectedSegmentIndex == 0) { textf2.text = "5"; } else if(segmentedcontrol.selectedSegmentIndex == 1) { textf2.text = "10"; } e ...
-
您将需要通过继承UIControl来创建自定义控件。 这里有个简单的例子: CustomSegmentedControl.swift import UIKit import CoreImage public class CustomSegmentedControl: UIControl { public var borderWidth: CGFloat = 1.0 public var selectedSegementIndex = 0 { didSet { ...
-
如何点击segmentedControl对象并使用它来改变视图?(How to click the segmentedControl object and use it to change view?)[2022-10-21]
将NSSegmentedControl用于分段控件,并使用tabless NSTabView在视图之间切换。 如果它在你的应用中有意义,你甚至可以将它们绑定到相同的用户默认值,并且在不写一行代码的情况下实现它。 Use NSSegmentedControl for the segmented control, and a tabless NSTabView to switch between views. If it makes sense in your app, you could even bind ... -
TableViewCell中的SegmentedControl - 重用单元格的正确方法(SegmentedControl in TableViewCell - Proper way to reuse cell)[2021-06-20]
1)这是正确的,但如果您使用带有原型单元格的故事板,或者如果您为CellIdentifier手动注册自定义单元类,那么您只需调用 [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 它将始终返回正确的单元格(请参阅文档 ) 2)您可以这样做,但为了简单起见,您甚至可以使用-1(或UISegmentedControlNoSegment )而不是2来表示新单元格的selectedIndex。 ... -
在segmentedControl上更改UITableView的内容更改了索引(Change content of UITableView on segmentedControl changed index)[2023-08-29]
不知道你的代码中是否遗漏了它,或者你只是忘了在你的问题上复制它,但是因为你是从代码中创建了segmentControl,所以你在某处添加了这个,对吧? [segmentedControl addTarget:self action:@selector(segmentControlValueChanged:) forControlEvents:UIControlEventValueChanged]; Not sure if its missin ... -
请试试这个例子。 在xib中第一个拖放和拖动段。 在.h文件中 IBOutlet UISegmentedControl *mySegmentedControl; 在.m文件中 - (void)viewDidLoad { [super viewDidLoad]; [mySegmentedControl setSelectedSegmentIndex:0]; [mySegmentedControl addTarget:self action:@selector(segmentSwitch:) ...
-
Swift:如何通过滑动使SegmentedControl通知值更改?(Swift:How to make SegmentedControl notice value change done by swiping?)[2023-01-04]
遗憾的是,您无法通过更改所选索引来触发更改的分段控制值 你可以打电话给你 segmentValueChanged(segmentedControl) 在......的最后 handleswipes() 更新了sampleTableReuse中的代码 请仅将此用作灵感,并尝试相应地修改您的代码 Got it func handleSwipes(sender:UISwipeGestureRecognizer) { if segmentControl.selectedIndex == 1 ... -
有关评论的未来参考:如果您想要cellForRow中的值,那么您应该在'segmentedControlChangedValue'中重新加载tableview 一般情况下,根据选择/操作设置单元格,您应该根据需要重新加载/ beginUpdates tableview For future reference from comment: If you want value in cellForRow, then you should reload the tableview inside that 'seg ...