我正在使用Codables
解析JSON数据。问题是我的几个编码键与变量名不同。为此,我使用了CodingKeys
枚举,这很简单,但是我必须写所有键,我不想要那样。我只想覆盖一些键,而不是全部。
这是JSON
{
"string_one" = "some string",
"string_two" = "some string",
"string_three_%s" = "some string",
}
class Strings: Codable{
var string_one: String?
var string_two: String?
var string_three: String?
enum CodingKeys: String, CodingKey {
case string_three = "string_three_%s"
}
}
更多说明
我知道添加case string_one, string_two
是可行的,但是假设我有1000个字符串,并且只想覆盖一个,我必须无缘无故地写999个案例。对我来说,这似乎不是明智的选择(无缘无故地写999个案例)
答案 0 :(得分:0)
到目前为止,这是不可能的。您必须在<?php
$string = 'I m Programmer';
$found="Prog";
if ( preg_match("~\b$found\b~",$string) ){
echo "Matched";
}else{
echo "No Match";
}
?>
中指定所有if ( preg_match("~\b".strtolower($found)."\b~",strtolower($string)) ){
echo "Matched";
}else{
echo "No Match";
}
。
对于其他人,您不需要显式指定cases
,因为CodingKeys
的原始类型为rawValue
,并且会选择enum CodingKeys
作为默认名称String
,即
case
答案 1 :(得分:0)
简短的答案是您无法做到。这只能通过将所有情况如下所示来完成:-
#{toasts}
将按原样选择键#include<vector>
vector<int> my_vec;
和enum CodingKeys: String, CodingKey {
case string_one, string_two
case string_three = "string_three_%s"
}
答案 2 :(得分:0)
选中此形式的模态类。 希望您能找到解决方案
* Create the application.
*/
public Game() {
initialize();
startGame();
players.add(human);
players.add(computer);
//restart();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//Main window
frame = new JFrame();
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\X270\\eclipse-workspace\\Snakes and Ladders\\src\\org\\snakesandladders\\logo.png"));
frame.setBounds(100, 100, 600, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.setResizable(false);
//call panels
createGamePanel();
createControlPanel();
}
/**
* Creating Board Panel
*/
private void createGamePanel() {
gamePanel = new JPanel();
board = new Board();
addBoardToPanel(board, gamePanel);
frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
}
public void addBoardToPanel(Board b, JPanel p) {
int rows = 5;
int cols = 6;
JPanel ContainerPanel = new JPanel(new GridLayout(5, 6));
for (int r=rows; r>0; r--){
if(r % 2 == 0) {
int rowLeft = r * cols;
for (int n = rowLeft, l = (rowLeft - cols); n>l; n--){
Square sq = b.getSquare(n-1);
ContainerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, BORDER_WIDTH));
ContainerPanel.add(sq);
}
}
else{
int rowLeft = ((r - 1) * cols) +1;
for (int n = rowLeft, l = (rowLeft + cols); n<l; n++){
Square sq =b.getSquare(n-1);
ContainerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, BORDER_WIDTH));
ContainerPanel.add(sq);
}
}
p.add(ContainerPanel, BorderLayout.CENTER);
}
}
/**
* Creating the control panel
*/
private void createControlPanel() {
controlPanel = new JPanel();
infoPanel = new JPanel();
playerPanel = new JPanel();
buttonPanel = new JPanel();
controlPanel.setLayout(new BorderLayout(20, 20));
controlPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
humanLabel = new JLabel(/*humanPlayer.getName()*/ " (You)");
humanLabel.setPreferredSize(new Dimension(175, 40));
humanLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
humanLabel.setHorizontalAlignment(SwingConstants.CENTER);
humanLabel.setBorder(new LineBorder(Color.DARK_GRAY, 2));
humanLabel.setBackground(Color.RED);
humanLabel.setOpaque(true);
humanLabel.setForeground(Color.WHITE);
playerPanel.add(humanLabel, BorderLayout.WEST);
diceButton = new JButton("Roll Dice");
diceButton.setPreferredSize(new Dimension(150, 40));
diceButton.setFont(humanLabel.getFont());
/*diceButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
playTurn(humanPlayer);
}
});*/
playerPanel.add(diceButton, BorderLayout.CENTER);
computerLabel = new JLabel(/*humanPlayer.getName() + */" (Computer)");
computerLabel.setPreferredSize(humanLabel.getPreferredSize()); //new Dimension(labelWidth, labelHeight)
computerLabel.setFont(humanLabel.getFont());
computerLabel.setHorizontalAlignment(SwingConstants.CENTER);
computerLabel.setBorder(humanLabel.getBorder());
computerLabel.setBackground(Color.BLUE);
computerLabel.setOpaque(true);
computerLabel.setForeground(humanLabel.getForeground());
playerPanel.add(computerLabel, BorderLayout.EAST);
diceLabel = new JLabel("Dice Value: ?");
diceLabel.setPreferredSize(new Dimension(515, 40));
diceLabel.setFont(humanLabel.getFont());
diceLabel.setHorizontalAlignment(SwingConstants.CENTER);
diceLabel.setBorder(humanLabel.getBorder());
diceLabel.setBackground(Color.CYAN);
diceLabel.setOpaque(true);
infoPanel.add(diceLabel, BorderLayout.NORTH);
startButton = new JButton("Start New Game");
startButton.setFont(humanLabel.getFont());
startButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent r){
restart();
}
});
buttonPanel.add(startButton);
exitButton = new JButton("Exit");
exitButton.setFont(humanLabel.getFont());
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
exit();
}
});
buttonPanel.add(exitButton);
controlPanel.add(infoPanel, BorderLayout.NORTH);
controlPanel.add(buttonPanel, BorderLayout.SOUTH);
controlPanel.add(playerPanel, BorderLayout.CENTER);
frame.getContentPane().add(controlPanel, BorderLayout.SOUTH);
}
答案 3 :(得分:0)
您必须设置所有编码键才能立即使用。
该类将仅对您在枚举CodingKeys
中描述的键进行编码/解码。
如果您有更多的字符串,建议您与服务器团队联系以创建字符串数组([String]
)代替此模式。
更新:由于值的数量很多,因此您应该选择[String]
答案 4 :(得分:0)
只是一个建议(未经测试),但是您可以将基类(扩展Codable)与不想覆盖的字段一起使用,然后使用仅包含您需要覆盖的名称的子类吗?即CodingKeys是否要求您为继承的字段定义值?