How do you parse JSON with a colon in the name? An

2019-02-25 21:55发布

For example: { "primary:title":"Little Red Riding Hood"}

My Parser in Java (Android) is always getting stuck because of the colon between primary and title. I can parse anything else with ease, I just need help in this.

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    TextView txtViewParsedValue;

    private JSONObject jsonObject;
    private JSONArray jsonArray;

    String [] titles, links, mediaDescriptions, mediaCredits, descriptions, dcCreators, pubDates, categories;
    String [] permalinks, texts;            // guid
    String [] rels, hrefs;
    String [] urls, media, heights, widths; // media:content

    String strParsedValue = "";

    private String strJSONValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        strJSONValue = readRawTextFile(this, R.raw.jsonextract);

        txtViewParsedValue = (TextView) findViewById(R.id.text_view_1);

        try {
            parseJSON();

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void parseJSON() throws JSONException
    {
        txtViewParsedValue.setText("Parse 1");

        jsonObject = new JSONObject(strJSONValue);
        jsonArray = jsonObject.getJSONArray("item");

        titles = new String[jsonArray.length()];
        links = new String[jsonArray.length()];
        permalinks = new String[jsonArray.length()];
        texts = new String[jsonArray.length()];
        mediaDescriptions = new String[jsonArray.length()];
        mediaCredits = new String[jsonArray.length()];
        descriptions = new String[jsonArray.length()];
        dcCreators = new String[jsonArray.length()];
        pubDates = new String[jsonArray.length()];
        categories = new String[jsonArray.length()];

        txtViewParsedValue.setText("Parse 2");

        for (int i=0; i<jsonArray.length(); i++)
        {
            JSONObject object = jsonArray.getJSONObject(i);

            titles[i] = object.getString("title");
            links[i] = object.getString("link");

            JSONObject guidObj = object.getJSONObject("guid");
            permalinks[i] = guidObj.getString("isPermaLink");
            texts[i] = guidObj.getString("text");
            //mediaDescriptions[i] = object.getString("media:description");
            //mediaCredits[i] = object.getString("media:credit");

                // *** THE PARSER FAILS IF THE COMMENTED LINES ARE IMPLEMENTED BECAUSE
                // OF THE : IN BETWEEN THE NAMES ***

            descriptions[i] = object.getString("description");
            //dcCreators[i] = object.getString("dc:creator");
            pubDates[i] = object.getString("pubDate");
            categories[i] = object.getString("category");   
        }



        for (int i=0; i<jsonArray.length(); i++)
        {
            strParsedValue += "\nTitle: " + titles[i];
            strParsedValue += "\nLink: " + links[i];
            strParsedValue += "\nPermalink: " + permalinks[i];
            strParsedValue += "\nText: " + texts[i];
            strParsedValue += "\nMedia Description: " + mediaDescriptions[i];
            strParsedValue += "\nMedia Credit: " + mediaCredits[i];
            strParsedValue += "\nDescription: " + descriptions[i];
            strParsedValue += "\nDC Creator: " + dcCreators[i];
            strParsedValue += "\nPublication Date: " + pubDates[i];
            strParsedValue += "\nCategory: " + categories[i];
            strParsedValue += "\n";
        }


        txtViewParsedValue.setText(strParsedValue);
    }

    public static String readRawTextFile(Context ctx, int resId)
    {
         InputStream inputStream = ctx.getResources().openRawResource(resId);

            InputStreamReader inputreader = new InputStreamReader(inputStream);
            BufferedReader buffreader = new BufferedReader(inputreader);
             String line;
             StringBuilder text = new StringBuilder();

             try {
               while (( line = buffreader.readLine()) != null) {
                   text.append(line);
                   //text.append('\n');
                 }
           } catch (IOException e) {
               return null;
           }
             return text.toString();
    }

1条回答
可以哭但决不认输i
2楼-- · 2019-02-25 22:20

For one, and to answer your question, there is no issue with JSONObject and the org.json.* classes parsing keys with colons in them if they're properly formed. The following unit test passed which means it was able to parse your example scenario:

public void testParsingKeysWithColons() throws JSONException {
    String raw = "{ \"primary:title\":\"Little Red Riding Hood\"}";
    JSONObject obj = new JSONObject(raw);
    String primaryTitle = obj.getString("primary:title");
    assertEquals("Little Red Riding Hood", primaryTitle);
}

Another suggestion is that using arrays of Strings for your data is clumsy and you'd be much better organized using a data structure to represent your objects. Instead of string arrays for titles, links, descriptions; use an object that has these properties and make a list of the objects. For example:

public class MyDataStructure {

    public String title;
    public String primaryTitle;
    public String link;
    public String mediaDescription;

    public static class Keys {
        public static String title = "title";
        public static String primaryTitle = "primary:title";
        public static String link = "link";
        public static String mediaDescription = "media:description";
    }

}

And then you can make a "translator" class that does all the parsing for you and returns a list of your object. This is much easier to work with and keep track of. You never have to think about data misaligning or having more or less data in one of your arrays than you expected. You also have a much easier time testing where the problem is if your input data is missing anything or any of your json is malformed.

public class MyDataStructureTranslator {

    public static List<MyDataStructure> parseJson(String rawJsonData) throws JSONException {

        List<MyDataStructure> list = new ArrayList<MyDataStructure>();
        JSONObject obj = new JSONObject(rawJsonData);
        JSONArray arr = obj.getJSONArray("item");

        for(int i = 0; i < arr.length(); i++) {
            JSONObject current = arr.getJSONObject(i);
            MyDataStructure item = new MyDataStructure();
            item.title = current.getString(MyDataStructure.Keys.title);
            item.primaryTitle = current.getString(MyDataStructure.Keys.primaryTitle);
            item.link = current.getString(MyDataStructure.Keys.link);
            item.mediaDescription = current.getString(MyDataStructure.Keys.mediaDescription);
            list.add(item);
        }

        return list;
    }

}
查看更多
登录 后发表回答