通过Mysql解析本机json

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过Mysql解析本机json相关的知识,希望对你有一定的参考价值。

我是本机反应的新手。我需要在我的react native主页中显示我的JSON,但是我不确定该怎么做。您对Json文件是否长有任何建议吗?我只想在替代列表视图中显示“ s_title”:“ hjkjhjk”,“ s_description”:“ jnkmjhnkl”

JSON


[{"fk_i_item_id":3,"fk_c_locale_code":"en_US","s_title":"hjkjhjk","s_description":"jnkmjhnkl"}]

我的React-Native主页面下面是我需要从Json文件读取的页面:

import * as WebBrowser from 'expo-web-browser';
import React from 'react';
import {
  Image,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  View,
  Button,
  Alert

} from 'react-native';

export default function HomeScreen() {
  return (
    <View style={styles.container}>
      <ScrollView
        style={styles.container}
        contentContainerStyle={styles.contentContainer}>
        <View style={styles.welcomeContainer}>
          <Image
            source={
              __DEV__
                ? require('../assets/images/robot-dev.png')
                : require('../assets/images/robot-prod.png')
            }
            style={styles.welcomeImage}
          />
        </View>

        <View style={styles.getStartedContainer}>
          <DevelopmentModeNotice />
          <View
            style={[styles.codeHighlightContainer, styles.homeScreenFilename]}>
              <Button
          onPress={test}
          title="get DB"
          color="#841584"
          accessibilityLabel="get more about this purple button"
        />
          </View>
        </View>
      </ScrollView>
    </View>
  );
}

HomeScreen.navigationOptions = {
  header: null,
};

function test(){
  fetch('http://***:3000/users')
    .then(response => response.json())
    .then(users => console.warn(users));
  //Alert.alert(response);
}

function DevelopmentModeNotice() {
  if (__DEV__) {
    const learnMoreButton = (
      <Text onPress={handleLearnMorePress} style={styles.helpLinkText}>
        About dp
      </Text>
    );
    return (
      <Text style={styles.developmentModeText}>

        You get more than three wishes ! {learnMoreButton}
      </Text>
    );
  }
}
function handleLearnMorePress() {
  WebBrowser.openBrowserAsync(
    'https://demandplace.com/'
  );
}



const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
  },
  developmentModeText: {
    marginBottom: 20,
    color: 'rgba(0,0,0,0.4)',
    fontSize: 14,
    lineHeight: 19,
    textAlign: 'center',
  },
  contentContainer: {
    paddingTop: 30,
  },
  welcomeContainer: {
    alignItems: 'center',
    marginTop: 10,
    marginBottom: 20,
  },
  welcomeImage: {
    width: 100,
    height: 80,
    resizeMode: 'contain',
    marginTop: 3,
    marginLeft: -10,
  },

  homeScreenFilename: {
    marginVertical: 7,
  },
  codeHighlightText: {
    color: 'rgba(96,100,109, 0.8)',
  },
  codeHighlightContainer: {
    backgroundColor: 'rgba(0,0,0,0.05)',
    borderRadius: 3,
    paddingHorizontal: 4,
  },
  getStartedText: {
    fontSize: 17,
    color: 'rgba(96,100,109, 1)',
    lineHeight: 24,
    textAlign: 'center',
  },
  tabBarInfoContainer: {
    position: 'absolute',
    bottom: 0,
    left: 0,
    right: 0,
    ...Platform.select({
      ios: {
        shadowColor: 'black',
        shadowOffset: { width: 0, height: -3 },
        shadowOpacity: 0.1,
        shadowRadius: 3,
      },
      android: {
        elevation: 20,
      },
    }),
    alignItems: 'center',
    backgroundColor: '#fbfbfb',
    paddingVertical: 20,
  },
  tabBarInfoText: {
    fontSize: 17,
    color: 'rgba(96,100,109, 1)',
    textAlign: 'center',
  },
  navigationFilename: {
    marginTop: 5,
  },
  helpContainer: {
    marginTop: 15,
    alignItems: 'center',
  },
  helpLink: {
    paddingVertical: 15,
  },
  helpLinkText: {
    fontSize: 14,
    color: '#2e78b7',
  },
});

答案

您可以实现自己的json解析,该解析将解析json并将其转换为您可以从中读取的特定对象类型,同时确保可以解决任何带有映射的边缘情况(空属性或不存在的属性)。就个人而言,在我的项目中,我使用库json2typescripthttps://github.com/dhlab-basel/json2typescript)将JSON对象映射到域对象。如果您使用的是纯js,我敢肯定会存在类似的库,因此,我建议您寻找类似的库,以替代实现自己的逻辑以从json映射到域模型的方法。

如果对此还有其他疑问,请告诉我。

另一答案

最简单的方法是..您的jsonobject [0] .s_title和jsonobject [0] .s_description。在分配它之前,请检查它是否未定义。就像是title = jsonobject [0] .s_title ===未定义? “”:jsonobject [0] .s_title

以上是关于通过Mysql解析本机json的主要内容,如果未能解决你的问题,请参考以下文章

如何在本机反应中解析json数据

片段中的 JSON 解析 [关闭]

将 Json 表解析为 NSMutableArray [重复]

解析:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项

如何使用 mysql 本机 json 函数生成嵌套的 json 对象?

如何格式化通过 AJAX 从 ASPX JSON 解析的日期?