欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

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

发布时间:2025/3/15 javascript 27 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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();

}

总结

以上是生活随笔为你收集整理的view 冒号作用 组件中属性_如何解析名称中带有冒号的JSON?安卓/ Java...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。