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
207 views
in Technique[技术] by (71.8m points)

How to Update JSON value using Java

I have below json, i want to update each and every value of that json but sometimes only one value

{ 
"msgType": "NEW",
"code": "205",
"plid": "PLB52145",
}

I've already tried to update using below code

FileReader reader = new FileReader(filePath);

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

System.out.println(jsonObject);


long id =Long.valueOf((String) idNewObj.get("plid"));
System.out.println(plid);


idNewObj.put("plid",PL809809809);

System.out.println(jsonObject);
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

To make transformation/filtering of JSON files, I'd suggest to use stream/event-oriented parsing and generating rather than any object mapping. Just an example, which uses simple and lightweight JSON parser https://github.com/anatolygudkov/green-jelly :

import org.green.jelly.AppendableWriter;
import org.green.jelly.JsonEventPump;
import org.green.jelly.JsonParser;

import java.io.StringWriter;
import java.io.Writer;

public class UpdateMyJson {
    private static final String jsonToUpdate = "{
" +
            ""msgType": "NEW",
" +
            ""code": "205",
" +
            ""plid": "PLB52145",
" +
            "}";

    public static void main(String[] args) {
        final StringWriter result = new StringWriter();

        final JsonParser parser = new JsonParser();
        parser.setListener(new MyJsonUpdater(result));
        parser.parse(jsonToUpdate); // if you read a file with a buffer,
        // call parse() several times part by part in a loop until EOF
        parser.eoj(); // and then call .eoj()

        System.out.println(result);
    }

    static class MyJsonUpdater extends JsonEventPump {
        private boolean isPlid;

        MyJsonUpdater(final Writer output) {
            super(new AppendableWriter<>(output));
        }

        @Override
        public boolean onObjectMember(final CharSequence name) {
            isPlid = "plid".contentEquals(name);
            return super.onObjectMember(name);
        }

        @Override
        public boolean onStringValue(final CharSequence data) {
            if (isPlid) {
                if ("PLB52145".contentEquals(data)) {
                    return super.onStringValue("PL809809809");
                }
            }
            return super.onStringValue(data);
        }
    }
}

Props:

  1. the file/data doesn't require to be loaded entirely into memory, you can process megs/gigs with no problems
  2. it works much more faster, especially for large files
  3. it's easy to implement any custom type/rule of transformation with this pattern

Both of standard Gson and Jackson libs also provide tokenizers to work with JSON in streaming manner.

UPDATED: JsonEventPump used


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

...