view 冒号作用 组件中属性_如何解析名称中带有冒号的JSON?安卓/ Java...

例如:{“ primary:title”:“小红帽”}

由于主要名称和标题之间存在冒号,因此我在Java(Android)中的解析器总是卡住.我可以轻松解析其他任何内容,我只需要帮助.

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

{

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

{

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值