我从代码中得到以下输出: {"列表":[{" X":" Y"},{"":" B"} ]}
相反,我希望得到输出 [{" X":" Y"},{"":" B"}]
代码如下。
public class Test {
List<Map> list = new ArrayList();
public static void main(String [] args){
Test t = new Test();
Map m1 = new HashMap();
m1.put("x","y");
t.list.add(m1);
Map m2 = new HashMap();
m2.put("a","b");
t.list.add(m2);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.NON_PRIVATE);
Writer writer = new StringWriter();
try {
objectMapper.writeValue(writer, t);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("The json is:\n"+writer.toString());
}
}
更新此问题 - 使其更上一层楼 给我:
{&#34;列表&#34;:[{&#34;映射&#34; {&#34; X&#34;:&#34; Y&#34;&#34; X 1&#34 ;:&#34; Y 1&#34;}},{&#34;映射&#34; {&#34; A1&#34;:&#34; B1&#34;&#34;&#34; :&#34; b&#34;}}]}
我想要{{&#34; x&#34;:&#34; y&#34;,&#34; x1&#34;:&#34; y1&#34;},{&# 34; A1&#34;:&#34; B1&#34;&#34;&#34;:&#34; b&#34;}]
public class Test {
public class Car{
Map map = new HashMap();
}
List<Car> list = new ArrayList();
public static void main(String [] args){
Test t = new Test();
Test.Car car = t.new Car();
Map m1 = new HashMap();
m1.put("x","y");
m1.put("x1","y1");
car.map = m1;
t.list.add(car);
car = t.new Car();
Map m2 = new HashMap();
m2.put("a","b");
m2.put("a1","b1");
car.map = m2;
t.list.add(car);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.NON_PRIVATE);
Writer writer = new StringWriter();
try {
objectMapper.writeValue(writer, t);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("The json is:\n"+writer.toString());
}
}
答案 0 :(得分:1)
您可能希望使用@JsonValue
注释,其文档说明:
类似于XmlValue的标记注释,表示带注释的“getter”方法的结果(这意味着签名必须是getter的结果;非void返回类型,没有args)将被用作序列化的单个值。实例。通常,value将是一个简单的标量类型(String或Number),但它可以是任何可序列化的类型(Collection,Map或Bean)。
这是一个有效的例子:
public class Test {
public static class Car {
Map map = new HashMap();
@JsonValue
public Map getMap() {
return map;
}
}
List<Car> list = new ArrayList();
@JsonValue
public List<Car> getList() {
return list;
}
public static void main(String[] args) throws IOException {
Test t = new Test();
Car car = new Car();
Map m1 = new HashMap();
m1.put("x", "y");
m1.put("x1", "y1");
car.map = m1;
t.list.add(car);
car = new Car();
Map m2 = new HashMap();
m2.put("a", "b");
m2.put("a1", "b1");
car.map = m2;
t.list.add(car);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
Writer writer = new StringWriter();
objectMapper.writeValue(writer, t);
System.out.println("The json is:\n" + writer.toString());
}
}
答案 1 :(得分:1)
我实现了import com.fasterxml.jackson.databind.JsonSerializable;在Car class上做了以下几点。
private void downloadImage(String urlStr) {
progressDialog = ProgressDialog.show(this, "", "Downloading CSV file from " + urlStr);
final String url = urlStr;
Intent intent = new Intent(getApplicationContext(), ChartScreen.class);
final ArrayList resultList = new ArrayList();
Thread downloadImage = new Thread() {
public void run() {
InputStream in = null;
Message msg = Message.obtain();
msg.what = 1;
try {
in = openHttpConnection(url);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
String[] row = csvLine.split(",");
resultList.add(row);
}
}catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}finally {
in.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}.start();
downloadImage.join();
**//**Start activity with a new task flag or you will get error****
Bundle b = new Bundle();
b.putParcelableArrayList("Data", resultList);
intent.putExtras(b);
progressDialog.dismiss();
startActivity(intent);
}
Bundle b = new Bundle();
b.putParcelableArrayList("Data", resultList);
intent.putExtras(b);
progressDialog.dismiss();
startActivity(intent);
这删除了map关键字。我不能像上面的代码库一样使用JsonValue我不允许在Map上使用getter而JsonValue不适用于非公共字段。 (或者我无法使它工作)