如何在Java中序列化JSON对象

如何在Java中序列化JSON对象

本文介绍了如何在Java中序列化JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Voldemort存储我的数据.我的关键字是单词,值是单词和URL的出现次数.例如:

Hi I am using Voldemort to store my data. My key is a word and values are number of occurrence of the word and the URL. For example:

key :question
value: 10, www.stackoverflow.com

我正在使用Json对象放置我的值.我的代码看起来像这样

I am using Json object to put my values.My code looks like this

import org.json.JSONObject;
import com.metaparadigm.jsonrpc.JSONSerializer;
import voldemort.client.ClientConfig;
import voldemort.client.SocketStoreClientFactory;
import voldemort.client.StoreClient;
import voldemort.client.StoreClientFactory;

public class ClientExample {
  public static void main (String [] args) {
    String bootstrapUrl = "tcp://localhost:6666";

    ClientConfig cc = new ClientConfig ();
    cc.setBootstrapUrls (bootstrapUrl);
    String[] valuePair = new String[2];
    int val = 1;
    StoreClientFactory factory = new SocketStoreClientFactory (cc);
    StoreClient client = factory.getStoreClient("test");
    JSONObject json = new JSONObject();
    json.put("occurence",val);
    json.put("url", "www.cnn.com");
    client.put("foo", json);
  }
}

我的store.xml看起来像这样

And my store.xml looks like this

<stores>
  <store>
    <name>test</name>
    <persistence>bdb</persistence>
    <routing>client</routing>
    <replication-factor>1</replication-factor>
    <required-reads>1</required-reads>
    <required-writes>1</required-writes>
    <key-serializer>
      <type>string</type>
    </key-serializer>
    <value-serializer>
      <type>java-serialization</type>
     <schema-info>"Compount Types"</schema-info>
    </value-serializer>
  </store>
</stores>

当我尝试运行代码时,出现以下异常:**

While i was trying to run the code i am getting following exception:**

**

请告诉我如何序列化JSON对象.谢谢.

Could you please tell me how to serialize JSON object.Thanks in advance.

推荐答案

看看 Gson 库.它对对象映射和序列化执行了很好的JSON.

Have a look at Gson library. It does nice JSON to object mapping and serialization.

这篇关于如何在Java中序列化JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:26