gpt4 book ai didi

json - 如何在Java中序列化JSON对象

转载 作者:行者123 更新时间:2023-12-04 13:35:07 24 4
gpt4 key购买 nike

嗨,我正在使用伏地魔来存储我的数据。我的关键字是单词,值是单词和URL的出现次数。例如:

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

我正在使用Json对象放置我的值。
我的代码看起来像这样
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看起来像这样
<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>

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

Exception in thread "main" voldemort.serialization.SerializationException: java.io.NotSerializableException: org.json.JSONObject at voldemort.serialization.ObjectSerializer.toBytes(ObjectSerializer.java:47) at voldemort.store.serialized.SerializingStore.put(SerializingStore.java:109) at voldemort.store.DelegatingStore.put(DelegatingStore.java:68) at voldemort.client.DefaultStoreClient.put(DefaultStoreClient.java:208) at voldemort.client.DefaultStoreClient.put(DefaultStoreClient.java:193) at ClientExample.main(ClientExample.java:27) Caused by: java.io.NotSerializableException: org.json.JSONObject at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at voldemort.serialization.ObjectSerializer.toBytes(ObjectSerializer.java:44)



**

您能告诉我如何序列化JSON对象吗?

最佳答案

看看Gson库。它对对象映射和序列化执行了不错的JSON。

关于json - 如何在Java中序列化JSON对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4262390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com