1,261
社区成员
发帖
与我相关
我的任务
分享
{"locationstream":[{"IdType":"IP","Timestamp":1475222328000,"datatype":"coordinates","location":{"x":313.0,"y":205.0,"z":23},"userid":["0ac94949"],"map":{"mapid":"23"}},{"IdType":"IP","Timestamp":1475222328000,"datatype":"coordinates","location":{"x":184.0,"y":170.0,"z":23},"userid":["0aaeafcd"],"map":{"mapid":"23"}},{"IdType":"IP","Timestamp":1475222328000,"datatype":"coordinates","location":{"x":485.0,"y":196.0,"z":23},"userid":["0a8db61e"],"map":{"mapid":"23"}}]}
{"IdType":"IP","Timestamp":1475222328000,"datatype":"coordinates","location":{"x":313.0,"y":205.0,"z":23},"userid":["0ac94949"],"map":{"mapid":"23"}}
import com.alibaba.fastjson.JSON
object Main {
def main(args: Array[String]): Unit = {
val jsonStr = "{\"IdType\":\"IP\",\"Timestamp\":1475222328000,\"datatype\":\"coordinates\",\"location\":{\"x\":313.0,\"y\":205.0,\"z\":23},\"userid\":[\"0ac94949\"],\"map\":{\"mapid\":\"23\"}}"
val json = JSON.parseObject(jsonStr)
val idType = json.getString("IdType")
val timestamp = json.getLong("Timestamp")
val dataType = json.getString("datatype")
val location = json.getJSONObject("location")
val x = location.getInteger("x")
val y = location.getInteger("y")
val z = location.getInteger("z")
val userid = json.getJSONArray("userid")
for( i <- 0.to(userid.size) ) {
println(userid.getString(i))
}
val map = json.getJSONObject("map")
val mapid = map.getString("mapid")
}
}
[/quote]
第一个json 就是一个键值对,他的值是个数组,然后又嵌套。求scala代码[/quote]
你json格式是固定的吧?那你这问题就很无脑了。。。估计你没做过web开发。。。
import com.alibaba.fastjson.JSON
object Main {
def main(args: Array[String]): Unit = {
val jsonStr = "{\"IdType\":\"IP\",\"Timestamp\":1475222328000,\"datatype\":\"coordinates\",\"location\":{\"x\":313.0,\"y\":205.0,\"z\":23},\"userid\":[\"0ac94949\"],\"map\":{\"mapid\":\"23\"}}"
val json = JSON.parseObject(jsonStr)
val idType = json.getString("IdType")
val timestamp = json.getLong("Timestamp")
val dataType = json.getString("datatype")
val location = json.getJSONObject("location")
val x = location.getInteger("x")
val y = location.getInteger("y")
val z = location.getInteger("z")
val userid = json.getJSONArray("userid")
for( i <- 0.to(userid.size) ) {
println(userid.getString(i))
}
val map = json.getJSONObject("map")
val mapid = map.getString("mapid")
}
}
[/quote]
第一个json 就是一个键值对,他的值是个数组,然后又嵌套。求scala代码[/quote]
你json格式是固定的吧?那你这问题就很无脑了。。。估计你没做过web开发。。。
import com.alibaba.fastjson.JSON
object Main {
def main(args: Array[String]): Unit = {
val jsonStr = "{\"IdType\":\"IP\",\"Timestamp\":1475222328000,\"datatype\":\"coordinates\",\"location\":{\"x\":313.0,\"y\":205.0,\"z\":23},\"userid\":[\"0ac94949\"],\"map\":{\"mapid\":\"23\"}}"
val json = JSON.parseObject(jsonStr)
val idType = json.getString("IdType")
val timestamp = json.getLong("Timestamp")
val dataType = json.getString("datatype")
val location = json.getJSONObject("location")
val x = location.getInteger("x")
val y = location.getInteger("y")
val z = location.getInteger("z")
val userid = json.getJSONArray("userid")
for( i <- 0.to(userid.size) ) {
println(userid.getString(i))
}
val map = json.getJSONObject("map")
val mapid = map.getString("mapid")
}
}
[/quote]多谢,已经解决了。json格式是这样的 {locationstream:【{IdType:XXX,XXX:XXX}】}
import com.alibaba.fastjson.JSON
object Main {
def main(args: Array[String]): Unit = {
val jsonStr = "{\"IdType\":\"IP\",\"Timestamp\":1475222328000,\"datatype\":\"coordinates\",\"location\":{\"x\":313.0,\"y\":205.0,\"z\":23},\"userid\":[\"0ac94949\"],\"map\":{\"mapid\":\"23\"}}"
val json = JSON.parseObject(jsonStr)
val idType = json.getString("IdType")
val timestamp = json.getLong("Timestamp")
val dataType = json.getString("datatype")
val location = json.getJSONObject("location")
val x = location.getInteger("x")
val y = location.getInteger("y")
val z = location.getInteger("z")
val userid = json.getJSONArray("userid")
for( i <- 0.to(userid.size) ) {
println(userid.getString(i))
}
val map = json.getJSONObject("map")
val mapid = map.getString("mapid")
}
}