java后台如何生成.json文件
这是生成后的样子{"JSChart":{"datasets":[{"type":"pie","id":"blue","data":[{"unit":"'A'","valu...
这是生成后的样子
{
"JSChart" : {
"datasets" : [
{
"type" : "pie",
"id" : "blue",
"data" : [
{
"unit" : "'A'",
"value" : "40"
},
{
"unit" : "'B'",
"value" : "16"
},
{
"unit" : "'C'",
"value" : "20"
}
]
}
],
"colorset" : [
"#99CDFB",
"#3366FB",
"#0000FA"
],
"optionset" : [
{
"set" : "setSize",
"value" : "600, 300"
},
{
"set" : "setTitle",
"value" : "'Phd Reference Chart'"
},
{
"set" : "setShowXValues",
"value" : "false"
},
{
"set" : "setLegend",
"value" : "'#99CDFB', 'Papers where authors found'"
},
{
"set" : "setLegend",
"value" : "'#3366FB', 'Papers which cite from other articles'"
},
{
"set" : "setLegend",
"value" : "'#0000FA', 'Papers which cite from news'"
}
]
}
} 展开
{
"JSChart" : {
"datasets" : [
{
"type" : "pie",
"id" : "blue",
"data" : [
{
"unit" : "'A'",
"value" : "40"
},
{
"unit" : "'B'",
"value" : "16"
},
{
"unit" : "'C'",
"value" : "20"
}
]
}
],
"colorset" : [
"#99CDFB",
"#3366FB",
"#0000FA"
],
"optionset" : [
{
"set" : "setSize",
"value" : "600, 300"
},
{
"set" : "setTitle",
"value" : "'Phd Reference Chart'"
},
{
"set" : "setShowXValues",
"value" : "false"
},
{
"set" : "setLegend",
"value" : "'#99CDFB', 'Papers where authors found'"
},
{
"set" : "setLegend",
"value" : "'#3366FB', 'Papers which cite from other articles'"
},
{
"set" : "setLegend",
"value" : "'#0000FA', 'Papers which cite from news'"
}
]
}
} 展开
展开全部
先生成map 然后用alibaba的fastjson,附件中的这个包,里头有你意想不到的API
例如:
Map map = ......;
map的结构和要生成的字符串结构一致就行,你的optionset在map里头的结构就是一个list
list optionset= new ArrayList();
Map temp = new HashMap();temp.put('set',xxx);temp.put('value',xxx);
list.add(temp);
....有多少个就添加多少,
然后把list添加到最外层的Map对象中;
map.put('optionset',list); key就是json字符中对应的key
拼装好了map然后就是下一句就搞定了
String josnStr = JSONObject.toJSONString(map);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以自己写 也就是个键值对的字符串
也可以用官方的jar包 ,能把java对象直接转化成json
也可以用官方的jar包 ,能把java对象直接转化成json
追问
可以具体点吗?写出来例子吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果用SpringMvc的话配置一下就可以了,默认返回json:
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="0" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="0" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
追问
我现在做那个统计图表,查出来list,但是生成图标要用json传值,可以后台直接生成json的吗?
追答
你是用SpringMvc框架吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询