user1028408
user1028408

Reputation: 1230

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?

Upvotes: 2

Views: 3730

Answers (1)

Rich
Rich

Reputation: 36806

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);
}

Upvotes: 1

Related Questions