【问题标题】:How to transform SimpleOrderedMap into JSON string or JSON object?如何将 SimpleOrderedMap 转换为 JSON 字符串或 JSON 对象?
【发布时间】:2011-01-26 01:08:13
【问题描述】:

有什么标准的方法吗?

【问题讨论】:

    标签: solr solrj


    【解决方案1】:

    简而言之:不,因为 JSON 没有原始的有序映射类型。

    第一步是确定您的客户端的要求,就解码 JSON 字符串而言。由于 JSON 规范没有有序的地图类型,因此您必须决定要使用的表示形式。您做出的选择将取决于客户的解码要求。

    如果您可以完全控制 JSON 字符串的解码,则可以简单地将对象按顺序编码到映射中,使用保证按照您传入的迭代器顺序序列化事物的 JSON 库。

    如果您不能保证这一点,则应自行提出声明。两个简单的例子是:

    交替列表:

    “[键 1,值 1,键 2,值 2]”

    键/值条目对象列表:

    “[{key: key1, val:value1}, {key: key2, val:value2}]”

    一旦你想出了这个表示,就很容易编写一个循环遍历你的 SimpleOrderedMap 的简单函数。例如:

    JSONArray jarray = new JSONArray(); for(Map.Entry e : simpleOrderedMap) { jarray.put(e.key()); jarray.put(e.value()); }

    【讨论】:

      【解决方案2】:

      Java 版本:

      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.Map.Entry;
      
      import org.apache.solr.common.util.NamedList;
      
      public class SolrMapConverter {
      
          @SuppressWarnings({ "unchecked", "rawtypes" })
          public static Object toMap(Object entry) {
              Object response = null;
              if (entry instanceof NamedList) {
                  response = new HashMap<>();
                  NamedList lst = (NamedList) entry;
                  for (int i = 0; i < lst.size(); i++) {
                      ((Map) response).put(lst.getName(i), toMap(lst.getVal(i)));
                  }
              } else if (entry instanceof Iterable) {
                  response = new ArrayList<>();
                  for (Object e : (Iterable) entry) {
                      ((ArrayList<Object>) response).add(toMap(e));
                  }
              } else if (entry instanceof Map) {
                  response = new HashMap<>();
                  for (Entry<String, ?> e : ((Map<String, ?>) entry).entrySet()) {
                      ((Map) response).put(e.getKey(), toMap(e.getValue()));
                  }
              } else {
                  return entry;
              }
              return response;
          }
      }
      

      【讨论】:

        【解决方案3】:

        向映射简单地添加字段是行不通的,因为可能存在复杂的对象(不序列化为 JSON)。

        这是一个很好的代码。

         protected static toMap(entry){
          def response
          if(entry instanceof SolrDocumentList){
              def docs = []
              response = [numFound:entry.numFound, maxScore:entry.maxScore, start:entry.start, docs:docs]
              entry.each {
                  docs << toMap(it)
              }
          } else
          if(entry instanceof List){
              response = []
              entry.each {
                  response << toMap(it)
              }
          } else
          if(entry instanceof Iterable){
              response = [:]
              entry.each {
                  if(it instanceof Map.Entry)
            response.put(it.key, toMap(it.value))
                  else
                    response.put(entry.hashCode(), toMap(it))
              }
          } else
         if (entry instanceof Map){
              response = [:]
              entry.each {
                  if(it instanceof Map.Entry)
            response.put(it.key, toMap(it.value))
                  else
                    response.put(entry.hashCode(), toMap(it))
              }
          } else  {
              response = entry
          }
          return response
        }
        

        【讨论】:

          猜你喜欢
          • 2018-01-06
          • 2019-08-27
          • 2020-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多