How to serialize JSON objects in Java

Hi, I'm using Voldemort to store my data My key is a word, and the value is the number of occurrences of the word and URL For example:

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

I'm using the JSON object to place my value 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); 
  } 
}

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>

When I try to run the code, I get the following exception:**

**

Can you tell me how to serialize JSON objects Thank you in advance

Solution

Look at the gson library It makes good JSON for object mapping and serialization

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>