第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Json 字符串映射轉(zhuǎn)換器,

Json 字符串映射轉(zhuǎn)換器,

白衣非少年 2021-11-17 10:45:44
我正在嘗試為嵌套的 JsonObject 編寫一個通用代碼來映射轉(zhuǎn)換。我有一個示例 JSONObject 作為{  "glossary": {    "title": "example glossary",    "GlossDiv": {      "title": "S",      "GlossList": {        "GlossEntry": {          "ID": "SGML",          "SortAs": "SGML",          "GlossTerm": "Standard Generalized \n Markup Language",          "GlossDef": {            "para": "A  DocBook.",            "GlossSeeAlso": [              "GML",              "XML"            ]          },          "GlossSee": "markup"        }      }    }  }}我想將其轉(zhuǎn)換為具有鍵值的地圖glossary.title = "example glossary",glossary.GlossDiv.title = "S",glossary.GlossDiv.GlossList.GlossEntry.ID ="SGML",glossary.GlossDiv.GlossList.GlossEntry.SortAs ="SGML",glossary.GlossDiv.GlossList.GlossEntry.GlossTerm="Standard Generalized Markup Language",glosary.GlossDiv.GlossList.GlossEntry.GlossDef.para ="A  DocBook.",glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso_0 = "GML",glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso_1 = "XML",glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSee = "markup"
查看完整描述

3 回答

?
哆啦的時光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗 獲得超6個贊

這是使用以下方法Map從 json 字符串中讀取的方法Jackson:


public final class JsonUtils {

    public static <T> Map<String, T> readMap(String json) throws Exception {

        if (json == null)

            return null;


        ObjectReader reader = new ObjectMapper().readerFor(Map.class);

        MappingIterator<Map<String, T>> it = reader.readValues(json);


        if (it.hasNextValue()) {

            Map<String, T> res = it.next();

            return res.isEmpty() ? Collections.emptyMap() : res;

        }


        return Collections.emptyMap();

    }

}

這是Map使用給定的實用程序方法從 json讀取的方法:


Map<String, String> map = flatMap(new LinkedHashMap<>(), "", JsonUtils.readMap(json));

最后,這是如何轉(zhuǎn)換Map為 required Map(可能這可以在 Jackson 引擎中完成,提供自定義反序列化器左右,但我不知道具體如何,這就是為什么我更容易手動實現(xiàn)它):


public static Map<String, String> flatMap(Map<String, String> res, String prefix, Map<String, Object> map) {

    for (Map.Entry<String, Object> entry : map.entrySet()) {

        String key = prefix + entry.getKey();

        Object value = entry.getValue();


        if (value instanceof Map)

            flatMap(res, key + '.', (Map<String, Object>)value);

        else

            res.put(key, String.valueOf(value));

    }


    return res;

}


查看完整回答
反對 回復(fù) 2021-11-17
?
侃侃無極

TA貢獻(xiàn)2051條經(jīng)驗 獲得超10個贊

Jackson JSON 是一個非??岬膸?,它為您完成了這項工作。我在下面寫了一個簡單的例子,但你應(yīng)該能夠?qū)⑺鼞?yīng)用到你的 JSONObject。


假設(shè)您有一個A.class屬性B.class,而該屬性又具有嵌套屬性C.class


@JsonPropertyOrder({ "b" })

class A {

    @JsonProperty("b")

    public B b;


    @JsonProperty("b")

    public B getB() {

        return b;

    }


    @JsonProperty("b")

    public void setB(B b) {

        this.b = b;

    }

}


@JsonPropertyOrder({ "c" })

class B {

    @JsonProperty("c")

    public C c;


    @JsonProperty("c")

    public C getC() {

        return c;

    }


    @JsonProperty("c")

    public void setC(C c) {

        this.c = c;

    }

}


@JsonPropertyOrder({ "d" })

class C {

    @JsonProperty("d")

    public String d;


    @JsonProperty("d")

    public String getD() {

       return d;

    }


    @JsonProperty("d")

    public void setD(String d) {

        this.d = d;

    }

}

您可以將嵌套的 JSONObject{"b":{"c":{"d":"test"}}}轉(zhuǎn)換成A.class這樣:


C c = new C();

c.setD("test");


B b = new B();

b.setC(c);


JSONObject obj = new JSONObject();

obj.put("b", b);

String jsonAsString = new Gson().toJson(obj);


A a = mapper.readValue(jsonAsString, A.class);

同樣,您應(yīng)該能夠?qū)?JSONObject 轉(zhuǎn)換為您想要的任何類型。希望這可以幫助


查看完整回答
反對 回復(fù) 2021-11-17
?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗 獲得超5個贊

import com.google.gson.JsonArray;

import com.google.gson.JsonElement;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;


public class JsonToMapConvertor {


    private static HashMap<String, Object> mapReturn = new HashMap<String, Object>();

    public static JsonParser parser = new JsonParser();


    public static void main(String[] args) throws Exception{


    String json ="add your Json";


       HashMap<String, Object> map = createHashMapFromJsonString(json,"");        

        for (Map.Entry<String, Object> entry : map.entrySet()) {            

          if(!entry.getValue().toString().contains("{"))  

                System.out.println(entry.getKey()+" : "+entry.getValue());

        }        


   }  


public static HashMap<String, Object> createHashMapFromJsonString(String json,String prefix) {


    JsonObject object = (JsonObject) parser.parse(json);   

    Set<Map.Entry<String, JsonElement>> set = object.entrySet();

    Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();

    while (iterator.hasNext()) {


        Map.Entry<String, JsonElement> entry = iterator.next(); 

        String key = entry.getKey();


        if(prefix.length()!=0){

            key = prefix + "."+key;

        }


        JsonElement value = entry.getValue();

        if (null != value) {            

            if (!value.isJsonPrimitive()) {

                if (value.isJsonObject()) {

                    mapReturn.put(key,value);

                    mapReturn.put(key, createHashMapFromJsonString(value.toString(),key));

                } else if (value.isJsonArray() && value.toString().contains(":")) {


                    List<HashMap<String, Object>> list = new ArrayList<>();

                    JsonArray array = value.getAsJsonArray();

                    if (null != array) {

                        for (JsonElement element : array) {

                            list.add(createHashMapFromJsonString(value.toString(),key));

                        }                 

                        mapReturn.put(key, list);

                    }

                } else if (value.isJsonArray() && !value.toString().contains(":")) {                    

                    mapReturn.put(key, value.getAsJsonArray());

                }              

            } else {

                mapReturn.put(key, value.getAsString());

            }

        }

    }

    return mapReturn;

    }

}


查看完整回答
反對 回復(fù) 2021-11-17
  • 3 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號