{
"id": 912345678901,
"age": 33,
"text": "How do I stream JSON in Java?",
"geo": null,
}
红色部分如果表示有可能为null,则在判断的时候需要如下name.equals("geo")&& reader.peek() != JsonToken.NULL
橙色部分要是想不解析其中的Json字段,需要在判断的时候加上一个else语句如下:else{reader.skipValue()}
然后是一些例子,都是Json的各种组合形式,不定时更新
1. 现实Json数据,后面每一个都是如此不再做任何的解析了,然后跟着就是对应的代码内容
{
"id": 912345678901,
"text": "How do I stream JSON in Java?",
"geo": null,
"user": {
"name": "json_newb",
"followers_count": 41
}
}
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
System.out.println("id:" + reader.nextLong());
} else if (name.equals("text")) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("user")) {
usernameObject();
} else {
reader.skipValue();
}
}
reader.endObject();
private void usernameObject() throws IOException{
reader.beginObject();
while (reader.hasNext()) {
String nxtName = reader.nextName();
if (nxtName.equals("name")) {
System.out.println("nxtName:" + reader.nextString());
} else if (nxtName.equals("followers_count")) {
System.out.println("nxtName:" + reader.nextInt());
}
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
System.out.println("id:" + reader.nextLong());
} else if (name.equals("text")) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("user")) {
usernameObject();
} else {
reader.skipValue();
}
}
reader.endObject();
private void usernameObject() throws IOException{
reader.beginObject();
while (reader.hasNext()) {
String nxtName = reader.nextName();
if (nxtName.equals("name")) {
System.out.println("nxtName:" + reader.nextString());
} else if (nxtName.equals("followers_count")) {
System.out.println("nxtName:" + reader.nextInt());
}
}
2.
[
{
"id": 912345678901,
"text": "How do I stream JSON in Java?",
"geo": null,
"user": {
"name": "json_newb",
"followers_count": 41
}
},
{
"id": 777777777888,
"text": "dfngsdnglnsldfnsl",
"geo": null,
"user": {
"name": "dsfgpd",
"followers_count": 24
}
}
]
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
System.out.println("id:" + reader.nextLong());
} else if (name.equals("text")) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("user")) {
usernameObject();
} else {
reader.skipValue();
}
}
reader.endObject();
}
while(reader.hasNext()){
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
System.out.println("id:" + reader.nextLong());
} else if (name.equals("text")) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("user")) {
usernameObject();
} else {
reader.skipValue();
}
}
reader.endObject();
}
reader.endArray();
3.
{
"statuses":[
{
"id": 912345678901,
"text": "How do I stream JSON in Java?",
"geo": null,
"user": {
"name": "json_newb",
"followers_count": 41
}
},
{
"id": 777777777888,
"text": "dfngsdnglnsldfnsl",
"geo": null,
"user": {
"name": "dsfgpd",
"followers_count": 24
}
}
]
}
reader.beginObject();
while(reader.hasNext())
{
String begin = reader.nextName();
if(begin.equals("statuses"))
{
array();
}
}
reader.endObject();
private void array() throws IOException{
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
System.out.println("id:" + reader.nextLong());
} else if (name.equals("text")) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("user")) {
usernameObject();
} else {
reader.skipValue();
}
}
reader.endObject();
}
reader.endArray();
}
private void usernameObject() throws IOException{
reader.beginObject();
while (reader.hasNext()) {
String nxtName = reader.nextName();
if (nxtName.equals("name")) {
System.out.println("nxtName:" + reader.nextString());
} else if (nxtName.equals("followers_count")) {
System.out.println("nxtName:" + reader.nextInt());
}
}
reader.endObject();
while(reader.hasNext())
{
String begin = reader.nextName();
if(begin.equals("statuses"))
{
array();
}
}
reader.endObject();
private void array() throws IOException{
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
System.out.println("id:" + reader.nextLong());
} else if (name.equals("text")) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
System.out.println("text:" + reader.nextString());
} else if (name.equals("user")) {
usernameObject();
} else {
reader.skipValue();
}
}
reader.endObject();
}
reader.endArray();
}
private void usernameObject() throws IOException{
reader.beginObject();
while (reader.hasNext()) {
String nxtName = reader.nextName();
if (nxtName.equals("name")) {
System.out.println("nxtName:" + reader.nextString());
} else if (nxtName.equals("followers_count")) {
System.out.println("nxtName:" + reader.nextInt());
}
}
reader.endObject();
}