Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
952 views
in Technique[技术] by (71.8m points)

parse recursively unknown json input structure in java

I'm trying to parse recursively unknown json input structure in java like the format below and trying to rewrite the same structure in another json.

Meanwhile I need to validate each & every json key/values while parsing.

{"Verbs":[{
    "aaaa":"30d", "type":"ed", "rel":1.0, "id":"80", "spoken":"en", "ct":"on", "sps":null
},{
    "aaaa":"31", "type":"cc", "rel":3.0, "id":"10", "spoken":"en", "ct":"off", "sps":null
},{
    "aaaa":"81", "type":"nn", "rel":3.0, "id":"60", "spoken":"en", "ct":"on", "sps":null
}]}

Please advice which json parser I can use for reading and writing unknown json content.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This way you can recursively parse JSON object:

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

public class JsonQuestion {

    public static void main(String[] args) {
        String input =  "{"Verbs":[{
" +
                "    "aaaa":"30d", "type":"ed", "rel":1.0, "id":"80", "spoken":"en", "ct":"on", "sps":null
" +
                "},{
" +
                "    "aaaa":"31", "type":"cc", "rel":3.0, "id":"10", "spoken":"en", "ct":"off", "sps":null
" +
                "},{
" +
                "    "aaaa":"81", "type":"nn", "rel":3.0, "id":"60", "spoken":"en", "ct":"on", "sps":null
" +
                "}]}";

        JsonObject jsonObject = JsonObject.readFrom(input);
        handleObject(jsonObject);
    }

    private static void handleValue(JsonObject.Member member, JsonValue value) {
        if (value.isArray()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println("array value ");
            recurseArray(value.asArray());
        } else if (value.isBoolean()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", boolean value = " + value.asBoolean());
        } else if (value.isNull()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", null value");
        } else if (value.isNumber()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", number value = " + value.asDouble());
        } else if (value.isObject()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", object value ");
            handleObject(value.asObject());
        } else if (value.isString()) {
            if (member != null) {
                System.out.print("name = " + member.getName());
            }
            System.out.println(", string value = " + value.asString());
        }
    }

    private static void handleObject(JsonObject object) {
        for (JsonObject.Member next : object) {
            JsonValue value = next.getValue();
            handleValue(next, value);
        }
    }

    private static void recurseArray(JsonArray array) {
        for (JsonValue value : array) {
            handleValue(null, value);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...