如何使用java jackson更新json文件中的值

时间:2017-02-08 09:05:09

标签: java json jackson

我的Json文件将如下所示。 (列表(列表))

<div>
    <div class="row">
        <a href="#" class="popper" data-toggle="popover"
            data-placement="bottom"><span class="glyphicon glyphicon-plus"></span>Create Channel</a>
        <div class="popper-content hide">
            <div class="form-group">
                <!-- ng-controller="createChannelCon" -->
                <div class="form-group">
                    <label>Channel name:</label>
                </div>
            <div class="form-group">
                <input ng-model="crtchannel.Name" type="text" placeholder="enter channel's name" maxlength="30"
                       class="form-control input-md" required />
            </div>
            <div class="form-group">
                <label>Description:</label>
            </div>
            <div class="form-group">
                <textarea cols="15" ng-model="crtchannel.Description" type="text"
                          placeholder="enter channel's description" maxlength="500"
                          class="form-control input-md" required></textarea>
            </div>
            <div>
                <input ng-model="crtchannel.Type" type="radio" name="chtype"
                       value="private" required /> Private<br> <input
                       ng-model="crtchannel.Type" type="radio" name="chtype"
                       value="public" /> Public<br>
            </div>
            <div class="form-group">
                <button ng-click="createBtn()" class="btn btn primary">Apply</button>
            </div>
        </div>
    </div>
    <script>
        $('.popper').popover({
            container : 'body',
            html : true,
            content : function() {
                return $(this).next('.popper-content').html();
            }
        });
    </script>
</div>
</div>

我想替换第3个列表中的值(第3个列表中的第5个索引)。即:[[0,1759,10.0,20.0,30.0,40.0,50.0,60.0,70.0,590,1759] [1,1533,10.0,20.0,30.0,40.0,50.0,60.0,70.0,590,1533] [2,2638,10.0,20.0,30.0,40.0,50.0,60.0,70.0,590,2638] [3,2639,10.0,20.0,30.0,40.0,50.0,60.0,70.0,590,2639] [4,2640,10.0,20.0,30.0,40.0,50.0,60.0,70.0,590,2640]]

我希望将值30.0更改为10.0。如何在不获取整个文件数据的情况下进行此更改。

1 个答案:

答案 0 :(得分:0)

据我所知,如果您想使用JSON,我担心您不能不读取整个文件。

试试这可以是任何帮助

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.ListIterator;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author Ravi P
 */
public class Test {

    public static void main( String[] args ) {

        try {

            List<List<Integer>> jsonParentArr = new ObjectMapper().readValue( new File( "C:\\tmp\\Details.json" ), new TypeReference<List<List<Integer>>>() {
            } ); // read the data from file and form a list

            System.out.println( jsonParentArr.get( 2 ) ); // array values before replacing 30

            ListIterator<Integer> childJsonArr = jsonParentArr.get( 2 ).listIterator(); // get the 3rd array

            while ( childJsonArr.hasNext() ) {

                if ( childJsonArr.next() == 30 ) { // replace if the value to 10.0 if the exsting value is 30

                    childJsonArr.set( 10 );
                }
            }

            System.out.println( jsonParentArr.get( 2 ) ); // array values after replacing 30

            try (BufferedWriter bWriter = new BufferedWriter( new FileWriter( "C:\\tmp\\Details.json" ) )) {

                bWriter.write( jsonParentArr.toString() ); // write back the result to same file

            } catch ( Exception e ) {

                e.printStackTrace();
            }

        } catch ( Exception e ) {

            e.printStackTrace();

        }
    }
}