简单的Java Json到数组

时间:2015-10-08 07:00:43

标签: java json

我痴迷于愚蠢的事情我会帮助他们,

我通过javascript中的字符串化对象获取此字符串:

.data 
    string: .asciiz "I Love MIPS    

.text

.globl main main

la $t0 string

OFFSET:

addi $t1 $t0 8 #store string address with a 8 bit offset in $t1 => "L"
lw $t3 ($t1) # load "L"

将此转换为对象数组的最简洁,最简单的方法是什么?

我已经开始做一些丑陋的事情,只需删除不需要的字符并执行以下操作:

{"63F67024-6FE1-D1B9-41D2-61156F11089A":0,"7cc8732e-d532-463e-9b5e-38fe14664b9e":1,"7CC40FFC-7BED-82DF-41C3-78C2BE8CD901":2,"f7344b33-860a-4934-b1f8-044b80a7b894":3,"31f65628-12b1-4363-848d-2bce07b8ac30":4,"7CF2DCA9-7BEC-8566-41A2-4898E5C110BC":5,"7D1C42ED-7BED-82FE-41D2-5045E9F0C13F":6,"D4EC2E5B-D807-2F30-41EA-6A4D9278BE81":7,"91ACF8F7-9516-F12F-41C1-BF57E6F223BE":8,"28d65730-9da0-457b-9d25-0f33628c0e5c":9,"57D44260-6D6D-E0E0-4171-71080149751C":10}

但我确信GSON的理想选择是更清洁,更简单的方法

2 个答案:

答案 0 :(得分:1)

您的JSON字符串看起来像一个简单的键/值对列表。如何将其转换为地图

public static void jsonToMap(String t) throws JSONException {

        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject jObject = new JSONObject(t);
        Iterator<?> keys = jObject.keys();

        while( keys.hasNext() ){
            String key = (String)keys.next();
            String value = jObject.getString(key); 
            map.put(key, value);

        }

        System.out.println("json : "+jObject);
        System.out.println("map : "+map);
    }

希望它有所帮助。

答案 1 :(得分:1)

杰克逊拥有ObjectMapper类,它也做了这个,但它映射的对象和键名和对象字段名应该相同,对于映射代码将是

导入以下包

org.codehaus.jackson.map.ObjectMapper;

呼叫将

ObjectMapper objectMapper = new ObjectMapper();
Yourclass classObj = objectMapper
                .readValue(jsonasString,Yourclass.class);