简体   繁体   中英

Parsing JSON key with colon in it

Consider this JSON string:

{
          "title": "value1",
          "link": "value2",
    "media:info": "value3"
}

I know how to parse title and link, but the parser isn't accepting media info because of the colon in the middle I think. Does anyone have any ideas?

Use JSONObject. I wrote the following tests using your example data and they passed.

public void testJsonParsing() throws JSONException {
    JSONObject manual = new JSONObject();
    manual.put("media:info", "value3");

    String rawData = "{ \"title\": \"value1\", \"link\": \"value2\", \"media:info\": \"value3\" }";
    JSONObject parsed = new JSONObject(rawData);

    String expected = "value3";
    String actual = manual.getString("media:info");
    assertEquals("Actual equals expected", expected, actual);

    actual = parsed.getString("media:info");        
    assertEquals("Actual equals expected", expected, actual);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM